Django管理员中的通用关系/通用外键 [英] Generic Relations/Generic Foreign Keys in the Django Admin

查看:127
本文介绍了Django管理员中的通用关系/通用外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been trying to display a Generic Foreign Key in the Django admin but can't get it working. I have a FullCitation class that can be linked to either a NonSupportedProgram or a SupportedProgram class. So, I have used a generic foreign key.

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

In the admin, I want users to only be able to select 'NonSupportedProgram' or 'SupportedProgram' from the content_type dropdown and then, from the object_id field, I need users to be able to select from a dropdown listing the existing NonSuportedPrograms or the existing Supported Programs, with the option of creating a new one. Is this possible? Where am I going wrong?

models.py

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


推荐答案

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

This is a module that renders GenericForeignKeys in the Django Admin:

https://github.com/lexich/genericrelationview

它不工作与无冲突jQuery设置(如Django CMS中的一样)。

It just doesn't work with a no conflict jQuery setup (like the one from Django CMS).

这篇关于Django管理员中的通用关系/通用外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆