【问题标题】:Generic Relations/Generic Foreign Keys in the Django AdminDjango Admin 中的通用关系/通用外键
【发布时间】:2015-04-03 05:50:06
【问题描述】:

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

在管理员中,我希望用户只能从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


【解决方案1】:

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

https://github.com/lexich/genericrelationview

它只是不适用于没有冲突的 jQuery 设置(比如来自 Django CMS 的设置)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-06
    • 2012-11-18
    • 1970-01-01
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 2011-12-14
    相关资源
    最近更新 更多