-->

With django-tables2 how do I display a model @prop

2019-08-07 04:20发布

问题:

I have a @property in my model which is basically a string of several values in the model combined.

  @property
    def mfg_link(self):
        return ''.join('http://mfg.com/model/' + str(self.model_numer))

I can add this mfg_link to the list_display on the admin model and it works fine, but its not showing up in the generated table when I pass the queryset to the table, the other fields show up fine.

This seems like something obvious, but unfortunately the couple of hours of searching didn't help.

Thanks a lot!

回答1:

Do it like this in Table class:

class TableClass(tables.Table):
   mfg_link = tables.Column('mfg_link', order_by='model_numer')

   class Meta:
     model = SomeModel
     sequence = 'model_numer', 'mfg_link'