skip to Main Content

Null is not an empty string!

select count(*) from table where xyz is null is not the same as select count(*) from table where xyz == ''!

If I have a unique constraint on a column, multiple NULL values can exist. Multiple empty strings cannot!

I know what the difference is. I don’t need an explanation. All I want to know is how to insert a NULL value in Django admin.

I have tried:

default=None
null=True

No I don’t want blank. I want NULL.

When I look at my schemas from my migrations, I clearly see the columns allow NULL.

So, what is the fiddly little syntax in dynamically typed python that seems to be burred on the by the SEO of people who don’t understand what constraints are?

This field is required.

No it’s not. I can clearly see that from the table structure.

2

Answers


  1. When Django sends an update to MySQL for a NULL field it does not insert it as NULL (when you are in the admin).

    e.g.
    You have an openid_key field, which is the url of the users openid url. This url has to be unique and should be marked as such for db integrity.

    You go into the admin and edit a user, they have no openid_key. You save the user, All is fine.

    You go into the admin and edit a different user, they have no openid_key, You save the user. SQL throws a fit about the key needs to be unique.

    Django tried to insert “” instead of NULL for this field.

    Login or Signup to reply.
  2. You have to provide blank=True as well, so django admin will allow you set empty strings.

    For example I tested it on this model:

    class NullFieldModel(models.Model):
        title = models.CharField(max_length=100, null=True, default=None, blank=True)
    

    and it worked with sqlite. Following query respond me with new model:

    SELECT * FROM testapp_nullfieldmodel WHERE testapp_nullfieldmodel.title IS NULL
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search