User
或者
Organization
class Project(models.Model):
# ...
client_type = models.ForeignKey(
'contenttypes.ContentType',
on_delete=models.SET_NULL,
blank=True,
null=True,
limit_choices_to=(
models.Q(app_label='users', model='user') |
models.Q(app_label='organizations', model='organization')
)
)
client_id = models.PositiveIntegerField(blank=True, null=True)
client = GenericForeignKey('client_type', 'client_id')
# ...
@admin.register(models.Project)
class ProjectAdmin(admin.ModelAdmin):
list_display = (
'__str__',
'client',
)
但是,我找不到一个简单的方法来在项目的实际更改页面中显示客户机的名称。我尝试了显而易见的方法:
fields = (
'name',
'client',
)
client
字段名无效。我读了
Using generic relations as an inline
Django文档的一部分,但他们的例子似乎…倒退了?
我只想显示客户的名字,不需要编辑。有什么建议吗?