blank=False,null=False not working in models code Django
-
30-06-2021 - |
Pregunta
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?
Solución
- 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.
- Use URLValidator
Otros consejos
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.'] }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow