Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django model validation not working

I have the following model:

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)

In the shell, when i give full_clean i get validation error but on save it s getting saved rather than throwing error. why is this so? i am using django1.3 and 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()
like image 353
Vivek S Avatar asked Aug 03 '12 04:08

Vivek S


People also ask

How do I display validation error in Django?

To display the form errors, you use form. is_valid() to make sure that it passes validation. Django says the following for custom validations: Note that any errors raised by your Form.

How does Django validate data?

Django forms submit only if it contains CSRF tokens. It uses uses a clean and easy approach to validate data. The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class. It returns True if data is valid and place all data into a cleaned_data attribute.

What can be used to validate all model fields if any field is to be exempted from validation provide it in the exclude parameter?

clean_fields() method documentation: This method will validate all fields on your model. The optional exclude argument lets you provide a list of field names to exclude from validation. It will raise a ValidationError if any fields fail validation.


1 Answers

The documentation is explicit about this:

Note that validators will not be run automatically when you save a model, but if you are using a ModelForm, it will run your validators on any fields that are included in your form.

It's your responsibility to call the clean methods before saving if you're not using a form.

Calling the model validators can be forced like this:

model_instance.full_clean()
like image 129
Daniel Roseman Avatar answered Oct 18 '22 13:10

Daniel Roseman