9

I've had a search around for this but haven't had much luck so looking for a bit of help. I'm trying to add some extra columns to a table defined by a model, using function definitions in the model. Here's what my code looks like now:

# models.py
class MyModel(models.Model):
    my_field = models.TextField()

    def my_function(self):
        # Return some calculated value based on the entry
        return my_value

# tables.py
class MyTable(tables.Table):

    my_extra_column = tables.Column(....)

    class Meta:
        model = MyModel

# views.py
table = MyTable(MyModel.objects.all())
RequestConfig(request).configure(table)
return render(request, ....)

My question is can I access my_function in the entries passed to MyTable so I can show the result of my_function in the custom my_extra_column column? I assume I need to be using accessors, but I can't see how I can access the queryset data using this. Thanks!

DrBuck
  • 822
  • 7
  • 22

1 Answers1

15

I figured it out in the end, it was actually not too hard after all :) So using my example above, in order to add a custom column using a function in the associated model you just use accessors ...

# models.py
class MyModel(models.Model):
    my_field = models.TextField()
    my_field_2 = models.IntegerField()

    def my_function(self):
        # Return some calculated value based on the entry
        return my_value

# tables.py
class MyTable(tables.Table):

    my_extra_column = tables.Column(accessor='my_function',
         verbose_name='My calculated value')

    class Meta:
        fields = ['my_field', 'my_field_2', 'my_extra_column']
        model = MyModel

The trouble comes if and when you want to be able to sort this data, because the function won't translate into any valid field in MyModel. So you could either disable sorting on this column using ordering=False or specify a set using order_by=('field', 'field2')

DrBuck
  • 822
  • 7
  • 22
  • 1
    I am also facing a same kind of issue. Can we sort on the basis of the accessor? It does not seem to work. Gives the error that unable to resolve 'name' to a field name. – abhinav Jun 27 '17 at 12:39
  • Well you could specify ordering on the column using `order_by=('my_field', 'my_field2')` if that worked... otherwise I guess the only way you could really sort using this calculated value would be to create a new field in the model and save it for each entry in the db. – DrBuck Jun 29 '17 at 07:34
  • I don't think storing it directly in DB is a good approach. By order_by do you mean entering order_by in the queryset? Because that gives an error when we to sort on our custom field. I followed this issue on Github but it also does not work. https://github.com/bradleyayers/django-tables2/issues/413 – abhinav Jun 29 '17 at 10:24
  • made my day..!...thanx a lot – ronit Jun 21 '18 at 13:20
  • [This page in the docs](https://django-tables2.readthedocs.io/en/latest/pages/ordering.html#table-order-foo-methods) gives more information about sorting a custom column. – Kevin Nov 07 '18 at 04:12