107👍
I suspect class UserForm
should be model form. You may want to change fields, but it should be derived from `ModelForm.
So change form definition to
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = [...] # list of fields you want from model
#or define fields that you want.
....
2👍
I override __init__() method incorrectly, without initial arguments, as show below
class MyForm(forms.ModelForm):
...
def __init__(self):
super(CaseForm, self).__init__()
...
And get this error as result
TypeError at /case/create
__init__() got an unexpected keyword argument ‘initial’
To fix it, I set arguments to __init__() and pass them when call super class __init__(), see result below
class MyForm(forms.ModelForm):
...
def __init__(self, *args, **kwargs):
super(CaseForm, self).__init__(*args, **kwargs)
...
- [Django]-How to access Enum types in Django templates
- [Django]-Django REST Framework: how to substitute null with empty string?
- [Django]-NumPy array is not JSON serializable
1👍
forms.py
Defines Fields in Square Brackets like fields=[‘field 1’, ‘field 2’,…]
class CustomerForm(forms.ModelForm):
class Meta:
model = Customer
fields = ['fname','lname','email','address','city','state','zip','username','password','age','mobile','phone']
- [Django]-Django – iterate number in for loop of a template
- [Django]-Gunicorn, no module named 'myproject
- [Django]-Why won't Django use IPython?
1👍
I solved a similar error I got when trying to save a form to a database. “Report() got an unexpected keyword argument ‘summary'”
The problem was that the field names in models.py and forms.py didn’t match.
In the class Report in models.py the name of the field was “summary_input”, but in forms.py it was named “summary”, so I changed the variable name in forms to match the one in models.
# models.py
class Report(models.Model):
summary_input = models.TextField()
# forms.py
class ReportForm(forms.Form):
summary = forms.CharField(widget=forms.widgets.Textarea)
# changed to
# summary_input = forms.CharField(widget=forms.widgets.Textarea)
- [Django]-Modulus % in Django template
- [Django]-Django url pattern – string parameter
- [Django]-How to use MySQLdb with Python and Django in OSX 10.6?