3
class Url(models.Model):
    url=models.URLField(verify_exists=True,max_length=200,blank=False,null=False)
    date=models.DateTimeField(auto_now_add=True)
    count=models.IntegerField(default=0)
    isspam=models.IntegerField(default=0)

This is my models code....and when i make an object with no arguments..the object is created and is saved to the DB even after writing blank=False,null=False and the URL is also not checked for existence.If i supply it a dead link, it works but it shouldn't!

What is the problem with my code?

Related Query: Now that in django 1.4, verify_exists has been deprecated...how can i check for validation in 1.4?

Dhiraj Thakur
  • 716
  • 10
  • 25
  • Have you re-created the DB after the change? – Rohan Sep 07 '12 at 13:45
  • @Rohan Yeah but the problem persists! – Dhiraj Thakur Sep 07 '12 at 13:46
  • If you are editing an existing model and you run sync_db, it won't update the table. Use `south` or similar to allow migrations. See http://stackoverflow.com/questions/1115238/django-model-sync-table – will-hart Sep 07 '12 at 14:13
  • Can you post `python manage.py sqlall ` and check these fields are not null? – Rohan Sep 08 '12 at 05:32
  • @Rohan : don't worry about it..i did it...btw i got the hints on how to do this by sqlall and dir(Url)....sometimes i forget about dir but it's a lifesaver. Thanks for your help :) – Dhiraj Thakur Sep 08 '12 at 08:37

2 Answers2

0

So i got it working by modifying my 'url' to url=models.URLField(verify_exists=True,max_length=200,default=None,blank=False,unique=True) and validating it by object.clean_fields().

If you don't use valid values or empty values it throws errors like this

ValidationError: {'url': [u'This URL appears to be a broken link.'] }

ValidationError: {'url': [u'This field cannot be blank.'] }
Dhiraj Thakur
  • 716
  • 10
  • 25
0
  1. As you've seen, django does not validate model on save() by default, but your database should've thrown an error when inserting NULL value, I would check the schema to be sure.
  2. Use URLValidator
Dmitry Shevchenko
  • 31,814
  • 10
  • 56
  • 62