2012-08-03 8 views
10

tengo el siguiente modelo:Django no trabajar

class CardInfo(models.Model): 
    custid = models.CharField(max_length=30, db_index=True, primary_key = True) 
    first_name = models.CharField(max_length=100) 
    last_name = models.CharField(max_length=100) 
    street = models.CharField(max_length=100) 
    building = models.CharField(max_length=100) 
    city = models.CharField(max_length=100) 
    state = models.CharField(max_length=100) 
    zipcode = models.CharField(max_length=100) 
    payment_method = models.CharField(max_length=100, null=True, blank=True) 
    amount = models.CharField(default = '0',max_length=10, null=True, blank=True) 
    valid_to_month = models.CharField(max_length=100, null=True, blank=True) 
    valid_to_year = models.CharField(max_length=100, null=True, blank=True) 
def __unicode__(self): 
    return "Cust ID %s" %(self.custid) 

En el shell, cuando doy full_clean me sale error de validación sino en guardarlo S ser salvo en lugar de error de tiro. ¿por qué esto es tan? Estoy utilizando django1.3 y Python 2.6:

c=CardInfo(custid="Asasasas") 
c.full_clean() 
Traceback (most recent call last): 
File "<console>", line 1, in <module> 
File "/usr/local/python2.6/lib/python2.6/site-packages/django/db/models/base.py", line 828, in full_clean 
raise ValidationError(errors) 
ValidationError: {'building': [u'This field cannot be blank.'], 'city': [u'This field cannot be blank.'], 'first_name': [u'This field cannot be blank.'], 'last_name': [u'This field cannot be blank.'], 'zipcode': [u'This field cannot be blank.'], 'state': [u'This field cannot be blank.'], 'street': [u'This field cannot be blank.']} 
c.save() 
+0

Se debe dar error de integridad. ¿Tus modelos/db están sincronizados correctamente? sqlall de su aplicación será útil. – Rohan

+0

Sí, lo he hecho ... no hay mejoría –

Respuesta

23

La documentación es explicit about this:

Tenga en cuenta que los validadores no se ejecute automáticamente cuando se guarda un modelo, pero si usted está usando un ModelForm, ejecutará sus validadores en cualquier campo que esté incluido en su formulario.

Es su responsabilidad llamar a los métodos de limpieza antes de guardar si no está utilizando un formulario.

Llamar a los validadores modelo se puede forzar de esta manera:

model_instance.full_clean()