My question is pretty much identical to this one: Saving Django ModelForm with a ForeignKey, however when I tried what the accepted answer suggested, I got the following error:
"FieldError at /myapp/adduser"
"Unknown field(s) (address1, address4, postcode, address3, address2) specified for Users"
My files are as follows:
models.py
from django.db import models
from django.forms import ModelForm
class Addresses(models.Model):
address1 = models.CharField(max_length=100)
address2 = models.CharField(max_length=100)
address3 = models.CharField(max_length=100)
address4 = models.CharField(max_length=100)
postcode = models.CharField(max_length=8)
class Users(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
email = models.CharField(max_length=100)
dob = models.DateField()
address = models.ForeignKey(Addresses)
class AddressForm(ModelForm):
class Meta:
model = Addresses
fields = ['address1', 'address2', 'address3', 'address4', 'postcode']
class UserForm(ModelForm):
class Meta:
model = Users
fields = ['first_name', 'last_name', 'email', 'dob']
views.py
from .models import AddressForm, Addresses, UserForm, Users
def adduser(request):
AddressInlineFormSet = inlineformset_factory(Addresses, Users, form=AddressForm)
if request.method == 'POST':
userForm = UserForm(request.POST)
if userForm.is_valid():
new_user = userForm.save()
addressInlineFormSet = AddressInlineFormSet(request.POST, request.FILES, instance=new_user)
if addressInlineFormSet.is_valid():
addressInlineFormSet.save()
return HttpResponseRedirect(reverse('/thanks/'))
else:
classificationformset = ClassificationInlineFormset(request.POST, request.FILES, instance=new_user)
else:
addressInlineFormSet = AddressInlineFormSet()
userForm = UserForm()
return render(request, 'myapp/adduser.html', locals())
I'm also unclear on whether there are any problems with 'adduser.html'. I've got the following:
adduser.html
<h1>Create a new user</h1>
<form action="adduser" method="post">
{% csrf_token %}
{{ userForm }}
{{ addressInlineFormSet }}
<input type="submit" value="Submit" />
</form>
Any guidance on how to fix the problem would be very much appreciated!
EDIT:
Here's the full traceback, as requested:
Traceback:
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/myname/mysite/myapp/views.py" in adduser
56. AddressInlineFormSet = inlineformset_factory(Addresses, Users, form=AddressForm)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/forms/models.py" in inlineformset_factory
1049. FormSet = modelformset_factory(model, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/forms/models.py" in modelformset_factory
847. error_messages=error_messages, field_classes=field_classes)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/forms/models.py" in modelform_factory
545. return type(form)(class_name, (form,), form_class_attrs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/forms/models.py" in __new__
257. raise FieldError(message)
Exception Type: FieldError at /myapp/adduser
Exception Value: Unknown field(s) (address3, address2, address4, postcode, address1) specified for Users