I want to update the Meta.fields dynamically. Is it possible to do it from the Form constructor? I tried the following but year doesn’t show up during the form generation. Only name and title are displayed.
class Author(models.Model):
name = ...
title = ...
year = ...
class PartialAuthorForm(ModelForm):
class Meta:
model = Author
fields = ('name', 'title')
def __init__(self, *args, **kwargs):
self.Meta.fields += ('year',)
No, that won’t work. Meta is parsed by – surprisingly – the metaclass, before you even get to
__init__.The way to do this is to add the field manually to
self.fields: