管理员中的Django Multiple GenericRelation related_name

时间:2012-03-10 17:29:22

标签: python django-admin

我想创建一个通用外键并在同一个模型中多次重复使用

问题是当我加载管理界面时,我得到了显示相同数据的2个内联字段。

我尝试使用fk_name传递模型中定义的related_name但没有运气

以下是任何帮助表示感谢的代码

model.py:

class TranslatedCharField(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey()

    class Meta:
        unique_together = ('object_id', 'lang')

    lang = models.CharField(max_length=255, choices=LANGUAGES)
    text = models.CharField(max_length=255)

class Evento(models.Model):
    slug = generic.GenericRelation(TranslatedCharField, related_name="slug")
    titolo = generic.GenericRelation(TranslatedCharField, related_name="title")

admin.py:

class TranslatedSlugInline(generic.GenericTabularInline):
    extra = 0
    model = TranslatedCharField
    verbose_name = "Slug"
    verbose_name_plural = "Slug"


class TranslatedTitoloInline(generic.GenericTabularInline):
    extra = 1
    model = TranslatedCharField
    verbose_name = "Titolo"
    verbose_name_plural = "Titolo"


class EventoAdmin(admin.ModelAdmin):
    inlines = [
    TranslatedSlugInline,
    TranslatedTitoloInline,
    ]

admin.site.register(Evento, EventoAdmin)

这是结果: enter image description here

正如您所看到的,TranslatedCharField列表与2个关系重复

2 个答案:

答案 0 :(得分:0)

unique_together = ('object_id', 'lang')

我想你想要

unique_together = ('content_type', 'object_id', 'lang')

因为object_id在模型中不是唯一的。

slug = generic.GenericRelation(TranslatedCharField, related_name="slug")
titolo = generic.GenericRelation(TranslatedCharField, related_name="title")

“related_name”是TranslatedCharField可以在另一个方向上访问的方式,即您希望它访问该Evento的属性名称。 “slug_event”和“title_event”更贴合。

答案 1 :(得分:0)

好的,我已经回顾了一些事情,结果如下:

class TranslatedCharField(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey()
    relation_field = models.CharField(max_length=255)

    class Meta:
        unique_together = ('object_id', 'lang', 'relation_field')

    lang = models.CharField(verbose_name="lingua", max_length=255, choices=LANGUAGES)
    text = models.CharField(verbose_name="testo", max_length=255)


def translatedFieldInlineAdminFormFactory(relation_field, translatemodel):
    class TranslatedFieldInlineAdminForm(forms.ModelForm):

        class Meta:
            model = translatemodel

        def clean(self):
            self.cleaned_data["relation_field"] = relation_field
            return self.cleaned_data

class TranslatedTabularInline(generic.GenericTabularInline):
    def __init__(self, *args, **kwargs):
        super(TranslatedTabularInline, self).__init__(*args, **kwargs)
        self.form = translatedFieldInlineAdminFormFactory(self._relation_field_filter, self.model)

    extra = 1
    max_num = len(LANGUAGES)
    exclude = ('relation_field',)
    model = None
    _relation_field_filter = None
    return TranslatedFieldInlineAdminForm

这样当我需要多个TranslatedCharField时,我会执行以下操作

在管理员:

class TranslatedSlugInline(TranslatedTabularInline):
    model = TranslatedCharField
    _relation_field_filter = "slug_event_portal"
    verbose_name = "Slug"
    verbose_name_plural = "Slug"

class TranslatedSlugInline(TranslatedTabularInline):
    model = TranslatedCharField
    _relation_field_filter = "slug2_event_portal"
    verbose_name = "Slug2"
    verbose_name_plural = "Slug2"

多亏了clean方法,每个内联都使用了特定的_relation_field_filter 这允许我至少保存它们区分并使用自定义查询集检索它们 我仍然在研究Evento的模型来重新检索特定的值集而不是整个相关的字段,但我想我现在不是太远了