user1170793
user1170793

Reputation: 220

Django not raising error when creating object without passing required fields

I have a Django model like this:

class Registration(models.Model):
    appId =models.CharField(max_length = 100)
    registeredUser = models.ForeignKey('auth.User', null=True, blank=True)

When I go to Django Shell interface using python manage.py shell and try creating a Registration object without passing appId field (which is a required field) like this:

Registration.objects.create(registeredUser = userobject)

It creates a Registration object without giving me error You can't set appId (a non-nullable field) to None!.How is that happening?why is it not giving me error?What I am doing wrong or I am missing something?

Upvotes: 1

Views: 882

Answers (1)

Mathieu Dhondt
Mathieu Dhondt

Reputation: 8914

The Registration object's appID will have a value of '' (i.e. an empty string).

Also, have a look at the docs for the null option, you'll see that absence of the null option does not mean that the field is non-nullable. Instead, null=True means that empty values are stored as NULL in the database.

If you want to avoid empty strings, either add a rule to that model field's validation, or via your forms.

Upvotes: 3

Related Questions