I have the following model definitions, see below.
models.py:
class Userstatus(开发者_高级运维models.Model):
label = models.CharField(...)
description = models.CharField(...)
class Foo(models.Model):
title = models.CharField(...)
visibility = models.ManyToManyField(Userstatus)
admin.py:
class FooAdmin(ModelAdmin):
list_display = ('id', 'title', )
admin.site.register(Foo, FooAdmin)
In the admin list view of "Foo" via FooAdmin the list_display list should include the "label"s from Userstatus so a column for each label will appear. I could create and call a method that creates the list for list_display.
But then no properties or callables actually exist that would allow me to return let's say a boolean for each label column, based on the visibility-many-to-many field.
What are my options? Should I try to intercept a callable or attribute request to Foo and create a boolean result on the fly? (Hitting the DB too often or making the columns sortable is another problem, but first things first).
Django documentation says ...
ManyToManyField fields aren't supported, because that would entail executing a separate SQL statement for each row in the table. If you want to do this nonetheless, give your model a custom method, and add that method's name to list_display. (See below for more on custom methods in list_display.)
Are you sure you want Userstatus to be a database table and not just a list of a few statuses that could be accessed through a "choices" tuple?
精彩评论