【发布时间】:2015-04-03 05:50:06
【问题描述】:
我一直试图在 Django 管理员中显示GenericForeignKey,但无法正常工作。我有一个FullCitation 类,可以链接到NonSupportedProgram 或SupportedProgram 类。所以,我使用了通用外键。
在管理员中,我希望用户只能从content_type 下拉列表中选择'NonSupportedProgram' 或'SupportedProgram',然后,从object_id 字段中,我需要用户能够从下拉列表中进行选择列出现有的NonSuportedPrograms 或现有的SupportedPrograms,并可选择创建一个新的。这可能吗?我哪里错了?
models.py
class FullCitation(models.Model)
# the software to which this citation belongs
# either a supported software program or a non-supported software program
limit = models.Q(app_label = 'myprograms', model = 'supportedprogram') | models.Q(app_label = 'myprograms', model = 'nonsupportedprogram')
content_type = models.ForeignKey(ContentType), limit_choices_to = limit, )
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
is_primary = models.BooleanField(help_text="Is this the Primary Citation for the software program?")
class Meta:
unique_together = ('content_type', 'object_id')
app_label = 'myprograms'
reversion.register(FullCitation)
class NonSupportedProgram(models.Model):
title = models.CharField(max_length=256, blank = True)
full_citation = generic.GenericRelation('FullCitation')
class Meta:
app_label = 'myprograms'
reversion.register(NonSBGridProgram)
class SupportedProgram(models.Model):
title = models.CharField(max_length=256, blank = True)
full_citation = generic.GenericRelation('FullCitation')
# and a bunch of other fields.....
admin.py
class FullCitationAdmin(reversion.VersionAdmin):
fieldsets = (
('Which Program', {
'fields': ('content_type', 'object_id', ),
}),
('Citation Information', {
'fields': ('is_primary',),
}),)
# autocomplete_lookup_fields = {
# 'generic': [['content_type', 'object_id']],
# }
# inlines = ['NonSupportedProgramInline', ]
list_display = ('content_object', 'is_primary',)
search_fields = ('content_object__title', )
# list_filter = ('content_object',)
【问题讨论】:
-
你解决过这个问题吗?
-
不,从来没有想过。不得不停止这种方法并做一些完全不同的事情。不完全是我想要的,但是哦。
标签: python django django-models django-admin generic-foreign-key