我有以下Django模型:
class Project(models.Model):
name = models.CharField(max_length=255, verbose_name="Project Name")
class ProjectMember(models.Model):
project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name="members")
user_or_group = models.Q(app_label='django.contrib.auth', model='User') | models.Q(app_label='django.contrib.auth', model='Group')
member_content_type = models.ForeignKey(ContentType, editable=False, on_delete=models.CASCADE, limit_choices_to=user_or_group)
member_object_id = models.PositiveIntegerField()
member = GenericForeignKey('member_content_type', 'member_object_id')
在django的管理员中,我想在ProjectMembers
模型的编辑页面中添加Project
。我这样尝试过:
class ProjectMemberInline(GenericStackedInline):
model = ProjectMember
ct_field = "member_content_type"
ct_fk_field = "member_object_id"
extra = 1
@admin.register(Project)
class ProjectAdmin(ModelAdmin):
inlines = [
ProjectMemberInline,
]
在Project
的管理页面中,我看到了额外的内联,但是表单中的唯一字段是(很奇怪)project
,尽管显然这应该是我正在编辑的项目。如果我使用StackedInline
而不是GenericStackedInline
,则不会显示project
,而是要求我输入membe_object_id
,这是正确的。
在这里使用GenericStackedInline
在做什么?