python - 在 Django 管理员中,添加通用关系的内联

标签 python django

这是我的简化模型:

from django.contrib.contenttypes.fields import (
    GenericForeignKey, GenericRelation)
from django.db import models
from django.utils.translation import ugettext_lazy as _


class Thing(models.Model):
    '''
    Our 'Thing' class
    with a link (generic relationship) to an abstract config
    '''

    name = models.CharField(
        max_length=128, blank=True,
        verbose_name=_(u'Name of my thing'))

    # Link to our configs
    config_content_type = models.ForeignKey(
        ContentType,
        null=True,
        blank=True)
    config_object_id = models.PositiveIntegerField(
        null=True,
        blank=True)
    config_object = GenericForeignKey(
        'config_content_type',
        'config_object_id')


class Config(models.Model):
    '''
    Base class for custom Configs
    '''
    class Meta:
        abstract = True

    name = models.CharField(
        max_length=128, blank=True,
        verbose_name=_(u'Config Name'))

    thing = GenericRelation(
        Thing,
        related_query_name='config')


class FirstConfig(Config):
    pass


class SecondConfig(Config):
    pass

这是管理员:
from django.contrib import admin
from .models import FirstConfig, SecondConfig, Thing


class FirstConfigInline(admin.StackedInline):
    model = FirstConfig


class SecondConfigInline(admin.StackedInline):
    model = SecondConfig


class ThingAdmin(admin.ModelAdmin):
    model = Thing

    def get_inline_instances(self, request, obj=None):
    '''
    Returns our Thing Config inline
    '''
        if obj is not None:
            m_name = obj.config_object._meta.model_name
            if m_name == "firstconfig":
                return [FirstConfigInline(self.model, self.admin_site), ]
            elif m_name == "secondconfig":
                return [SecondConfigInline(self.model, self.admin_site), ]
        return []


admin.site.register(Thing, ThingAdmin)

到目前为止,我有一个 Thing带有 FirstConfig 的对象对象链接在一起。
代码被简化:在一个不相关的部分,我设法创建了我的抽象 ConfigThing创建和设置正确 content_type/object_id .

现在我想看这个 FirstConfig作为内联 ( FirstConfigInline ) 的实例在我的 ThingAdmin 中.

我尝试使用 django.contrib.contenttypes.admin.GenericStackedInline ,尽管它不适用于我当前的模型设置。
我试着玩弄 fk_name我的 FirstConfigInline 的参数.
另外,正如你所看到的,我试图玩弄“东西”GenericRelation我的 Config 上的属性模型,没有成功..

关于如何继续正确设置管理员的任何想法?

最佳答案

根据Django Docs如果 ct_fk_field 和 ct_field 已从默认值更改,则必须定义它们。因此,将 ct_field 设置为 config_content_type 可能就足够了。

希望它有效!

编辑:这些值必须在内联中声明:

class SecondConfigInline(admin.StackedInline):
    model = SecondConfig
    ct_fk_field = "config_object_id"
    ct_field = "config_content_type"

编辑2:

我刚刚意识到我的假设有误。通常你应该在内联模型上声明外键。根据您的代码的其余部分,您可以删除 Thing 上的通用外键 + Config 上的 genericRelation,并在 Config-Basemodel 上声明一个正常的外键。

关于python - 在 Django 管理员中,添加通用关系的内联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46920460/

相关文章:

python - 在 Windows 上使用 PIP 编译 mysql-python

python - 如何运行 Python 文件作为 C 文件的参数?

python - 在OpenCV Python中查找旋转矩形

python - DRY 调用 serializer.data 不会返回模型的更新实例

python - Django 多数据库支持是否仅为数据库中的某些特定应用程序生成表

python - 具有多个连接的 Django ORM 查询

python - 从 QDockWidget 附加和分离外部应用程序时的问题

Python 3.6 - 在包含字典的列表中搜索单词,列表中包含字典

python - 如何从与当前登录用户关联的模型导入和编辑变量

python - 使用 HyperlinkedModelSerializer 强制 https?