Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Positivly insert NULL (not blank!) with Django

Tags:

null

django

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.

like image 761
Alexander Kleinhans Avatar asked Nov 25 '25 23:11

Alexander Kleinhans


1 Answers

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
like image 156
Gasanov Avatar answered Nov 28 '25 02:11

Gasanov