While it is probably not possible to change how Django treats this, many newcomers find it super confusing. To make developer experience in our Oracle-based project better, we started using the following workaround for fields which are required to be not null and not empty string:
class NonEmptyCharField(models.CharField):
"""
CharField preventing empty string and null
It workarounds two problems:
- CharField has a default value '' which allows silent save of model without an error even
if we forget to set mandatory fields. This is solved by setting the default to None causing
db error on not null constraint.
- The above is not sufficient for Oracle as the db backend has hardcoded null=True regardless
of what we set. This is changed by empty_strings_allowed=False.
"""
empty_strings_allowed = False
def __init__(self, *args, **kwargs):
kwargs.setdefault('default', None)
super().__init__(*args, **kwargs)
Dne čtvrtek 8. listopadu 2018 13:43:25 UTC+1 Florian Apolloner napsal(a):