我正在使用 Django 1.7 和 factory_boy 创建一些模型。这是代码:
在模型.py 中:
class Address(models.Model):
first_line = models.CharField(max_length=50, blank=True)
second_line = models.CharField(max_length=50, blank=True)
city = models.CharField(max_length=30, blank=True)
state = models.CharField(max_length=2, blank=True)
zipcode = models.CharField(max_length=5, blank=True)
zipcode_ext = models.CharField(max_length=4, blank=True)
factory.py(同一目录)中的关联工厂:
class AddressFactory(factory.django.DjangoModelFactory):
class Meta:
model = Address
first_line = "555 Main St."
second_line = "Unit 2"
city = "Chicago"
state = "IL"
zipcode = "60606"
zipcode_ext = "1234"
现在,在 django shell 中给出这段代码:
>>> from models import Address
>>> from django.forms.models import modelform_factory
>>> AddressForm = modelform_factory(Address)
>>> from factories import AddressFactory
>>> a = AddressFactory.create()
>>> af = AddressForm(instance = a)
>>> af.is_valid()
False
出于某种原因,对 is_valid 的调用似乎总是返回 false,我不知道为什么。表单上似乎没有任何错误,并且 clean()、clean_fields() 和 validate_unique() 似乎都不会在实例上引发错误。
为什么 is_valid 总是返回 false?