Django,抽象模型类+派生模型&表格“排除”字段问题

时间:2012-12-03 06:46:49

标签: django model abstract

class SomeBaseClass(models.Model):
    CHOICES_1 = (
     (1, '1'),
     (2, '2'),
     (3, '3'),
     (4, '4'),
     )
    ONETIME = 'OT'
    WEEKLY = 'WK'
    BIWEEKLY = 'BW'
    MONTHLY = 'MN'
    BIMONTHLY = 'BM'
    YEARLY = 'YR'
    CHOICES_2 = (
        (ONETIME, 'One Time'),
        (WEEKLY, 'Weekly'),
        (BIWEEKLY, 'Bi-Weekly'),
        (MONTHLY, 'Monthly'),
        (BIMONTHLY, 'Bi-Monthly'),
        (YEARLY, 'Yearly'),
    )
    CHOICES_3 = (
         (0,'0'),
         (1,'1'),
         (2,'2'),
         (3,'3'),
         (4,'4'),
         (5,'5'),
         (10,'10'),
    )
    field1 = models.CharField(max_length=10)
    field2 = models.DateTimeField(auto_now_add=True) 
    field3 = models.DateField()
    field4 = models.TimeField()
    field5 = models.SmallIntegerField(choices=CHOICES_1)
    field6 = models.CharField(max_length=2,choices=CHOICES_2)
    field7 = models.SmallIntegerField(choices=CHOICES_3)
    field8 = models.CharField(max_length=80)
    field9 = models.TextField(max_length=300)
    class Meta:
        abstract = True

class SomeDerivedClass(SomeBaseClass):
    field10 = models.DateTimeField()
    field11 = models.ForeignKey(Business, blank=True)


class SomeDerivedClassForm(forms.ModelForm):
    class Meta:
        model = SomeDerivedClass
        exclude = ['field2']
        exclude = ['field10']
        exclude = ['field11']

当" SomeDerivedClassForm"即使我已将其设置为排除,现场10也会显示在网站上。在此之前,我在基类中有field11,那时field10没有以渲染的形式显示,但是field11即使被排除在外也显示出来。我意识到我的代码中必定有一些错误,但是我没看到它是什么 您是否需要做一些特殊的事情来使用派生类的表单?

1 个答案:

答案 0 :(得分:2)

我不确定这是那么简单。

在您的代码中,不是在此处,每次要向其添加字段时,都会覆盖exclude列表。

 exclude = ['field2']
 exclude = ['field10']
 exclude = ['field11']

写:

 exclude = ['field2', 'field10', 'field11']