Django Admin中的通用关系/通用外键

时间:2015-02-03 21:20:37

标签: python django django-models django-admin generic-foreign-key

我一直在尝试在Django管理员中显示通用外键但无法使其正常工作。我有一个FullCitation类,可以链接到NonSupportedProgram或SupportedProgram类。所以,我使用了通用外键。

在管理员中,我希望用户只能从content_type下拉列表中选择“NonSupportedProgram”或​​“SupportedProgram”,然后从object_id字段中,我需要用户能够从列出现有NonSuportedPrograms的下拉列表中进行选择或现有的受支持程序,可选择创建新程序。这可能吗?我哪里错了?

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',)

1 个答案:

答案 0 :(得分:0)

这是一个在Django Admin中呈现GenericForeignKeys的模块:

https://github.com/lexich/genericrelationview

它不适用于没有冲突的jQuery设置(就像Django CMS中那样)。