Django Admin:使用相同的模型自动生成多个内联

san*_*nic 5 python django admin

我正在写我的第一个Django应用程序.我有以下数据库模型:

class Person(models.Model):
    first_name          = models.CharField(max_length = 100)
    last_name           = models.CharField(max_length = 100)

class InformationType(models.Model):
    name = models.CharField(max_length = 100)

class Information(models.Model):

    person       = models.ForeignKey(Person)
    info_type    = models.ForeignKey(InformationType)
    info         = models.CharField(max_length = 200)
Run Code Online (Sandbox Code Playgroud)

我想通过按类型拆分信息模型并动态地创建Django Admin(类PersonAdmin(ModelAdmin))中的多个内联.此外,我想从用户界面隐藏(排除)字段'info_type'并自动用相应的值填充它.

我可以使用'info_type'过滤的'信息'数据动态创建内联,但在UI中隐藏此字段会使其在保存时为空.

我该怎么做?是否可以制作隐藏的字段?或者我应该存储'info_type'值?

我用Google搜索并没有发现任何东西=)

附:好的.我修改了'信息'类:

class Information(models.Model):

    def save(self, *args, **kwargs):
        self.info_type = self.fixed_info_type
        super(Information, self).save(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)

...并制作了几个代理人:

class InformationManager(models.Manager):

    def __init__(self, info_type, *args, **kwargs):
        self.__info_type = info_type
        super(InformationManager, self).__init__(*args, **kwargs)

    def get_query_set(self, *args, **kwargs):
        return super(self.__class__, self).get_query_set(*args, **kwargs).filter(info_type=self.__info_type)

class PhoneInformation(Information):

    fixed_info_type = 'PHONE'
    objects = InformationManager(fixed_info_type)
    class Meta:
        proxy = True

class EmailInformation(Information):

    fixed_info_type = 'EMAIL'
    objects = InformationManager(fixed_info_type)
    class Meta:
        proxy = True
Run Code Online (Sandbox Code Playgroud)

在admin.py中:

from contacts.models import Person, PhoneInformation, EmailInformation 
class PersonAdmin(admin.ModelAdmin):
    __inlines = []

    class classproperty(property):
        def __get__(self, instance, owner):
            return super(self.__class__, self).fget.__get__(None, owner)()

    @classproperty    
    @classmethod
    def inlines(cls):

        def get_inline(InformationModel):
            class Inline(admin.TabularInline):
                model = InformationModel
                exclude= ['info_type']

            return Inline

        if not cls.__inlines:
            for InformationModel in [PhoneInformation, EmailInformation]:
                cls.__inlines.append(get_inline(InformationModel))
        return cls.__inlines
Run Code Online (Sandbox Code Playgroud)

所以它看起来很丑陋并且不符合DRY原则.我无法以与InlineAdmin相同的方式创建代理.它每次都会返回相同的对象.

Fra*_*llo 1

我过去做过的事情是:

创建各种内联表单,覆盖每个表单的查询集,以仅允许按类型划分的信息的特定子集

(看看这个灵感:限制为 django admin Inline 显示的条目的查询集