With django-tables2 how do I display a model @property?

我只是一个虾纸丫 提交于 2020-01-05 10:08:14

问题


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'


来源:https://stackoverflow.com/questions/31736973/with-django-tables2-how-do-i-display-a-model-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!