我正在尝试验证模型,但我收到此错误The Member could not be created because the data didn't validate.
失败的行是employee = form.save(commit = False)
。
这是我的表格:
class EmployeeForm(forms.ModelForm):
class Meta:
model = Member
fields = ['first_name', 'last_name', 'customer_code', 'address1', 'address2', 'city', 'state', 'zip_code', 'telephone']
def __init__(self, *args, **kwargs):
super(EmployeeForm, self).__init__(*args, **kwargs)
self.fields['first_name'].widget = TextInput(attrs={
'placeholder': 'First Name'})
self.fields['last_name'].widget = TextInput(attrs={
'placeholder': 'Last Name'})
self.fields['customer_code'].widget = TextInput(attrs={
'placeholder': 'Customer Code'})
self.fields['address1'].widget = TextInput(attrs={
'placeholder': 'Address'})
self.fields['address2'].widget = TextInput(attrs={
'placeholder': 'Address Line 2'})
self.fields['city'].widget = TextInput(attrs={
'placeholder': 'City'})
self.fields['state'].widget = TextInput(attrs={
'placeholder': 'State'})
self.fields['zip_code'].widget = TextInput(attrs={
'placeholder': 'Zip Code'})
self.fields['telephone'].widget = TextInput(attrs={
'placeholder': 'Telephone'})
def is_valid(self):
#super(EmployeeForm, self).clean()
if 'customer_code' in self.errors:
del self._errors['customer_code']
return self
这是我的观点:
@login_required
def employee_register(request):
if request.method == 'POST':
form = EmployeeForm(request.POST)
if form.is_valid():
print "FORM IS VALID"
employee = form.save(commit = False)
code = employee.customer_code
try:
employer = Member.objects.get(customer_code = code)
print "GOT EMPLOYER"
employee.employer = employer
except ObjectDoesNotExist:
print "NO EMPLOYER FOUND"
error = "The customer code entered does not match a valid customer in our database."
return render_to_response('app/employee_register.html', {'form' : form, 'error' : error}, context_instance = RequestContext(request))
employee.user = request.user
employee.role = "Attendee"
employee.customer_code = None
employee.save()
print "SAVED USER"
return HttpResponseRedirect(reverse('app:mainRouter'))
else:
print "FORM NOT VALID"
return render_to_response('app/employee_register.html', {'form' : form}, context_instance = RequestContext(request))
else:
form = EmployeeForm()
return render_to_response('app/employee_register.html', {'form' : form}, context_instance = RequestContext(request))
和我的模特:
class Member(models.Model):
first_name = models.CharField(max_length = 30)
last_name = models.CharField(max_length = 30)
user = models.OneToOneField(User, null = False)
email = models.EmailField(null = False)
address1 = models.CharField(max_length=100)
address2 = models.CharField(max_length=100, blank=True, null = True)
city = models.CharField(max_length=100)
state = models.CharField(max_length=2)
zip_code = models.CharField(max_length=5)
country = models.CharField(max_length=50, null = True, blank = True)
telephone = models.CharField(max_length = 15)
role_choices = (
('Attendee', 'Attendee'),
('Customer', 'Customer'),
('Registrar', 'Registrar'),
('Area Coordinator', 'Area Coordinator'),
('Admin', 'Admin'),
('Sales', 'Sales'),
)
role = models.CharField(choices = role_choices, max_length = 50)
employer = models.ForeignKey('Member', null = True, blank = True)
## ATTENDEE FIELDS
#employer_code = models.CharField(max_length = 50, null = True)
electrosign = models.BooleanField(default = False)
##CUSTOMER FIELDS
customer_code = models.CharField(max_length = 50, null = True, unique = True, blank = True)
insurance = models.ForeignKey('Insurance', null = True, blank = True)
def __unicode__(self):
name = self.first_name + self.last_name
return name
我不确定问题是什么,我在尝试保存对象之前检查表单is_valid()
。感谢所有帮助/建议。
答案 0 :(得分:0)
您必须不覆盖is_valid
。
您已完成此操作的方式,您从self
返回is_valid
,由于这不是None,False或空字符串或列表,因此它被视为True:所以您的表单始终"有效"。
自定义验证的正确方法是覆盖clean
方法,这就是你应该在这里做的; clean
的正确返回值为self.cleaned_data
。