django display content of a manytomanyfield

后端 未结 1 1463
离开以前
离开以前 2020-12-31 19:24

I started using django framework just a few days ago and i desperately need some help with my application.

It consists of client,project,admin,and admin payment clas

1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-31 20:20

    Displaying the contents of ManyToMany field isn't supported by default by django, because the database will be queried for each row of the results. You can display it yourself by adding a method to your Project-model:

    class Project(models.Model):
        ....
        def admin_names(self):
            return ', '.join([a.admin_name for a in self.admins.all()])
        admin_names.short_description = "Admin Names"
    

    and put admin_names in your list_display fields!

    0 讨论(0)
提交回复
热议问题