I'm trying to use the modelformset_factory to render multiple forms. However, when trying to run the server, this error occurs TypeError: modelform_factory() got an unexpected keyword argument 'extra'
All the online sources say that I should be able to specify the extra
argument in the modelform_factory but I can't seem to.
forms.py
class MapSeniorTeachForm(ModelForm):
role = forms.CharField(max_length=32)
def __init__(self, teach_id, *args, **kwargs):
super(MapSeniorTeachForm, self).__init__(*args, **kwargs)
self.teach_id = teach_id
self.fields['senior'] = forms.ChoiceField(choices=[(1,1),(2,2),(3,3)])
class Meta:
model = MapSeniorTeach
fields = ['role', 'senior']
MapSeniorTeachFormset = modelform_factory(MapSeniorTeach, form=MapSeniorTeachForm, extra=1)
Clipped Output Log
File "C:\Users\ethan\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "C:\Users\ethan\Documents\VSCodeProjects\bansheeApp\bansheeApp_project\banshee\training\urls.py", line 3, in <module>
from .views import *
File "C:\Users\ethan\Documents\VSCodeProjects\bansheeApp\bansheeApp_project\banshee\training\views.py", line 14, in <module>
from .forms import LessonTeachForm, ActivityTeachForm, MapSeniorTeachFormset
File "C:\Users\ethan\Documents\VSCodeProjects\bansheeApp\bansheeApp_project\banshee\training\forms.py", line 25, in <module>
MapSeniorTeachFormset = modelform_factory(MapSeniorTeach, form=MapSeniorTeachForm, extra=1)
One thing to note, if I remove the extra keyword, one instance of the form is successfully rendered.
Here are some sources I was speaking about before:
https://docs.djangoproject.com/en/4.1/topics/forms/formsets/
https://micropyramid.com/blog/understanding-djangos-model-formsets-in-detail-and-their-advanced-usage/
Any help is greatly appreciated!
As @Iain Shelvington said in the comments:
I miswrote modelform_factory
instead of the correct modelformset_factory