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']
When the “SomeDerivedClassForm” is rendered, the field10 shows up on the website even though I’ve set it to be excluded. Prior to this I had field11 in the base class, and at that time field10 was not shown in the rendered form but field11 was showing up even though it’s excluded. I realize I must have some bug in my code but I’m not seeing what it is.
Do you need to do anything special to use forms with derived classes?
I’m not sure this is that simple.
Instead of this in your code, here you are overwriting the
excludelist every time you want to add a field to it.write: