2012-08-03 11 views
10

Ho il seguente modello:Django non funziona

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) 

Nella shell, quando do full_clean ottengo errore di convalida, ma il risparmio è s sempre salvato, piuttosto che errore di lancio. perché è così? sto usando django1.3 e 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

Dovrebbe dare errore di integrità. I tuoi modelli/db sono sincronizzati correttamente? sarà utile la sqlall della tua app. – Rohan

+0

sì, l'ho fatto ... nessun miglioramento –

risposta

23

La documentazione è explicit about this:

Nota che validatori non verrà eseguito automaticamente quando si salva un modello, ma se si utilizza un ModelForm, eseguirà i tuoi validatori su tutti i campi che sono inclusi nel modulo.

È responsabilità dell'utente chiamare i metodi puliti prima di salvare se non si utilizza un modulo.

Chiamando i validatori modello può essere costretto in questo modo:

model_instance.full_clean()