Hello everyone, I am trying to utilize the SIngleTableMixin of 
django_tables2 using a custom function that queries my database and 
aggregates the data into a list of dictionaries.  I read to use the 
SingleTableMixin that all I needed was an iterable containing a data 
structure that has a key(a list of dicts) and then to instantiate a table 
in tables.py and pass to a my views Class.  I am getting an error when I do 
this TypeError: Object() takes no parameters.  I have included a link to 
the django tables 2 doc and my code below:

#views.py
from django_tables2.views import SingleTableMixin
from django_tables2.export.views import ExportMixin
from django_filters.views import FilterView
from django.views.generic.list import ListView
from django.shortcuts import render

from finance.models import Circuitinfotable, Budget
from .forms import monthlyCostForm
from .tables import CircuitTable, monthlyCostTable
from .filters import CircuitinfotableFilter

from datetime import datetime
from decimal import Decimal
from re import sub
import calendar

#convert a distinct queryset into a singular list of values
def getList(qs, key):
    resultsList = []
    for each in qs:
        resultsList.append(each[key])
    return resultsList


def getDistinct(key):
    return Circuitinfotable.objects.all().values(key).distinct()


def convertCurrency(val):
    return Decimal(sub(r'[^\d.]', '', val))


def getMonthlyCostQuery(year, field):
    circuits = Circuitinfotable.objects.all()
    field_list = getList(getDistinct(field), field)

    results_list = []
    resultsDict={'field': 'None', 'Jan': 0, 'Feb': 0, 'Mar': 0, 'Apr': 0, 
'May': 0, 'Jun': 0, 'Jul': 0, 'Aug': 0, 'Sep': 0, 'Oct': 0, 'Nov': 0, 
'Dec': 0,
                 'Janbw': 0, 'Febbw': 0, 'Marbw': 0, 'Aprbw': 0, 'Maybw': 
0, 'Junbw': 0, 'Julbw': 0, 'Augbw': 0, 'Sepbw': 0, 'Octbw': 0, 'Novbw': 0, 
'Decbw': 0}
    results_list.append(resultsDict)
    for each in field_list:
        if each!=None and each!='None':
            resultsDict={'field': '', 'Jan': 0, 'Feb': 0, 'Mar': 0, 'Apr': 
0, 'May': 0, 'Jun': 0, 'Jul': 0, 'Aug': 0, 'Sep': 0, 'Oct': 0, 'Nov': 0, 
'Dec': 0,
                         'Janbw': 0, 'Febbw': 0, 'Marbw': 0, 'Aprbw': 0, 
'Maybw': 0, 'Junbw': 0, 'Julbw': 0, 'Augbw': 0, 'Sepbw': 0, 'Octbw': 0, 
'Novbw': 0, 'Decbw': 0}
            resultsDict['field'] = each
            results_list.append(resultsDict)

    for circuit in circuits:
        #If you add a field to forms.py to aggregate by then add here
        if field=='region':
            matchField = circuit.region
        elif field=='carrier':
            matchField = circuit.carrier
        elif field=='bandwidth':
            matchField = circuit.bandwidth
        elif field=='status':
            matchField = circuit.status
        #get the budget for each circuit
        for budgetItem in circuit.budget_set.filter(yearnum=year):
            #if an item is budgeted for given month
            if(budgetItem.actualmrc!=0):
                #Cycle through results_list to find the correct dictionary
                for each in results_list:
                    if each['field']==matchField:
                        #update budgetItem and bw
                        if budgetItem != None:
                            
each[calendar.month_abbr[budgetItem.monthnum]]+=convertCurrency(budgetItem.actualmrc)
                        if circuit.bw!= None:
                            each[calendar.month_abbr[budgetItem.monthnum] + 
'bw']+=int(circuit.bw)
                    elif matchField==None:
                        if budgetItem != None:
                            
results_list[0][calendar.month_abbr[budgetItem.monthnum]]+=convertCurrency(budgetItem.actualmrc)
                        if circuit.bw!= None:
                            
results_list[0][calendar.month_abbr[budgetItem.monthnum]+'bw']+=int(circuit.bw)

    results_list = sorted(results_list, key=lambda i:i['field'])
    return results_list


class MonthlyCost(SingleTableMixin):
    template_name = 'functions/ClassMonthlyCost.html'
    context_object_name = 'qs'
    queryset = monthlyCostTable(getMonthlyCostQuery(2020, 'region'))


#tables.py
import django_tables2 as tables
from django_tables2.utils import A
from .models import Circuitinfotable

class CircuitTable(tables.Table):
    class Meta:
        model = Circuitinfotable
        export_formats = ['csv', 'xlsx']
        template_name = "django_tables2/bootstrap.html"
        Id1 = tables.Column(linkify={"viewname": "viewLit", "args": 
[tables.A('id1__pk')]})
        fields = ("id1", "circuitid", "bandwidth", "region", "carrier", 
"segmentname", "status", "mrcnew", "diversity", )

class monthlyCostTable(tables.Table):
    field = tables.Column()
    Jan = tables.Column()
    Janbw = tables.Column()
    Feb = tables.Column()
    Febbw = tables.Column()
    Mar = tables.Column()
    Marbw = tables.Column()
    Apr = tables.Column()
    Aprbw = tables.Column()
    May = tables.Column()
    Maybw = tables.Column()
    Jun = tables.Column()
    Junbw = tables.Column()
    Aug = tables.Column()
    Augbw = tables.Column()
    Sep = tables.Column()
    Sepbw = tables.Column()
    Oct = tables.Column()
    Octbw = tables.Column()
    Nov = tables.Column()
    Novbw = tables.Column()
    Dec = tables.Column()
    Decbw = tables.Column()

Documentation Link that I am using:
https://django-tables2.readthedocs.io/en/latest/pages/table-data.html 

Anybody know where I went wrong??? If I can figure this out I really feel 
like I can utilize django and django_tables2 to really improve! 

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/7ebd4978-3044-411d-bba9-5e2942ee850an%40googlegroups.com.

Reply via email to