#32799: Inconsistency regarding the default value of CharField
-------------------------------------+-------------------------------------
Reporter: Adam | Owner: nobody
Sołtysik |
Type: | Status: new
Uncategorized |
Component: Database | Version: 3.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Let's assume I want to add a `CharField` to a model:
{{{
class Test(Model):
# other fields
name = CharField(max_length=20)
}}}
This field is non-nullable and has no default value. However, when an
object is created, the field gets an empty string automatically:
{{{
>>> Test().name
''
}}}
When such an object is saved, an empty string will be saved to the
database. The same happens when I call `Test.objects.create()`.
On the other hand, when I create a migration, Django asks me to provide a
default. If I omit a default value and try to execute the migration, there
will be `IntegrityError` due to the `NOT NULL` constraint.
So the question is: why does `CharField` behave differently in the
contexts of Python objects and migrations? The same goes also for
`FileField`, but not e.g. for non-nullable `IntegerField` (it won't get a
value of `0` automatically).
Changing the behaviour of objects with `CharField` would be a breaking
change, but for consistency, I think that migrations should also infer the
empty string as the default value, even when there is no `default=''` or
`blank=True`.
Possibly related: #23405.
--
Ticket URL: <https://code.djangoproject.com/ticket/32799>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/050.491ed21bfade3bc63b6c4de99c140770%40djangoproject.com.