Reputation: 969
I'm trying to get some frequency data out from my queryset in a column in my table in django-tables2.
I am trying this via a custom field:
class MyTable(tables.Table):
custom_field = tables.Column(empty_values=())
def render_custom_field(self):
total_queryset = self.data.data
# trying to get out the row object, to do a filter query something like:
row_object = self.rows.instance
freq = total_queryset.filter(myfield=row_object.myfield)
return f"{len(freq)}/{len(total_queryset)}"
However I cannot get the row instance out to do this query. Does anyone know how I can get the row instance out to do this? (I'm also aware getting the total_queryset from self.data.data is probably a hacky way of doing this...)
Thanks!
Upvotes: 2
Views: 421
Reputation: 2557
record
is available as a direct argument, as well if your data is a QuerySet you can also use the .count()
method.
def render_custom_field(self, record):
return self.data.data.filter(myfield=record.myfield).count()
Upvotes: 1
Reputation: 969
Figured it out - you get the row instances from kwargs:
def render_custom_field(self, *args, **kwargs):
total_queryset = self.data.data
obj = kwargs['record']
freq = total_queryset.filter(myfield=row_object.myfield)
return len(freq)
Upvotes: 0