CharField and Textfield default constraints?

Hi all,

A basic premise, and maybe a bad one.

Having the data constrained at the lowest level possible gives us the strongest guarantees of its quality.

In “CharField” and “TextField” fields, if “blank=False” (default),
then we expect some value to be entered, not an empty character (‘’).

However, this is only checked by Django when clean() is called.

It would be nice if the ORM automatically added the constraint to the SQL:

# model
# ...
myfield = models.CharField(max_length=11)
# ...

# SQL
# ...
CONSTRAINT "myfield_not_empty" CHECK (NOT ("myfield" = '')),
# ...

Is there a principled or technical obstacle to this?

Hi ardaijozsef,

Take a look at check constraints: Constraints reference | Django documentation | Django

I know Django’s documentation on this. Thank you.

My original question was probably not fully understood. Sorry.

Django ORM does a lot of things automatically to make life easier for developers.

It occurred to me, why shouldn’t such a basic thing be done automatically by the ORM?

So if the field property is ‘blank=False’, the constraint is automatically created in SQL behind the scenes.

In the same way that the “max_length=11” property is automatically set to “varchar(11)” in SQL (also an auto-created constraint).

In this way, the developer would not have to manually set this for dozens of fields. Shorter code, saving time and energy. After all, this is the essence of it all.

So maybe my original question is understandable.
Is there any principled or technical obstacle for the ORM to do this automatically?

I think the biggest constraint here is backwards compatibility. If we added the database-level constraint to CharField now, it would break many existing projects. Also blank is really a form-level attribute, making it apply to the database is somewhat questionable.

It’s probably best we consider making it possible to create field subclasses which add constraints to the model. Then you could make something like NonEmptyCharField for your project.

An implementation limitation is that fields can’t currently add constraints to the model they’re attached to, in contribute_to_class. Adding this capability has been discussed fairly recently but I can’t find the ticket right now.

Thank you very much. This is now completely understandable.

Also blank is really a form-level attribute, making it apply to the database is somewhat questionable.

Yes, I know that, the question was hypothetical.

It’s probably best we consider making it possible to create field subclasses which add constraints to the model. Then you could make something like NonEmptyCharField for your project.

That would be a very good solution. Even better, because of the field subclasses.
I think many of us think this would be a useful feature.

An implementation limitation is that fields can’t currently add constraints to the model they’re attached to, in contribute_to_class . Adding this capability has been discussed fairly recently but I can’t find the ticket right now.

I hope that this can be realized.

Yeah I’ve been wanting to request this for a while now :sweat_smile: We might’ve touched on it on that thread in this forum about column-based check constraints … but I can’t seem to find it.

Based on this guide, I had attempts.

Unfortunately, I keep failing with it. I don’t see enough of the inner workings of django to do this. :slight_smile:

Thanks to both of you though.