我的问题与此问题完全相同:Saving Django ModelForm with a ForeignKey,但是当我尝试接受的答案建议时,我收到以下错误:
" / myapp / adduser"
中的FieldError"为用户指定的未知字段(地址1,地址4,邮政编码,地址3,地址2)"
我的文件如下:
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())
我还不清楚&adduser.html'是否有任何问题。我得到了以下内容:
adduser.html
<h1>Create a new user</h1>
<form action="adduser" method="post">
{% csrf_token %}
{{ userForm }}
{{ addressInlineFormSet }}
<input type="submit" value="Submit" />
</form>
非常感谢任何有关如何解决问题的指导!
编辑:
根据要求,这里是完整的追溯:
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