6

我正在编写我的第一个 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)

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

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

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

我用谷歌搜索了一下,一无所获=)

附:好的。我修改了“信息”类:

class Information(models.Model):

    def save(self, *args, **kwargs):
        self.info_type = self.fixed_info_type
        super(Information, self).save(*args, **kwargs)

...并做了一些代理:

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

在 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

所以它看起来很丑,不符合 DRY 原则。我不能像 InlineAdmin 那样创建代理。它每次都返回相同的对象。

4

2 回答 2

2

我找到了解决方案。现在我可以动态创建动态代理模型。模型.py:

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

class InformationType(models.Model):
    class Meta:
        ordering = ['order']

    name    = models.CharField(max_length = 200)
    order   = models.IntegerField()

class Information(models.Model):

    person       = models.ForeignKey(Person)
    info_type    = models.ForeignKey(InformationType)
    value        = models.CharField(max_length = 200)

    @classmethod    
    def getProxy(cls, info_type):
        class InformationManager(models.Manager):
            def get_query_set(self, *args, **kwargs):
                return super(self.__class__, self).get_query_set(*args, **kwargs).filter(info_type = info_type)

        def save(self, *args, **kwargs):
            self.info_type = info_type
            super(Information, self).save(*args, **kwargs)

        class Meta:
            proxy = True

        return type(
                    'Information'+str(info_type.pk), 
                    (Information,),
                    {
                        'Meta': Meta,
                        'save': save,
                        '__module__': 'contacts.models',
                        'objects': InformationManager(),

                     }
                    )

在 admin.py 中:

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(info_model):
            class Inline(admin.TabularInline):
                model = info_model
                exclude= ['info_type']
            return Inline

        if not cls.__inlines:
            for info_model in [Information.getProxy(info_type) for info_type in InformationType.objects.all()]:
                cls.__inlines.append(get_inline(info_model))
        return cls.__inlines
于 2012-08-14T13:29:23.377 回答
1

我过去做过的事情是:

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

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

于 2012-08-10T13:38:27.033 回答