1
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”时,即使我已将其设置为排除,field10 也会显示在网站上。在此之前,我在基类中有 field11,当时 field10 未显示在呈现的表单中,但 field11 出现了,即使它被排除在外。我意识到我的代码中一定有一些错误,但我没有看到它是什么。
您是否需要做一些特殊的事情才能将表单与派生类一起使用?

4

1 回答 1

2

我不确定这是否那么简单。

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

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

写:

 exclude = ['field2', 'field10', 'field11']
于 2012-12-03T07:22:22.610 回答