Project

Profile

Help

Issue #3446

closed

Using null=True with CharField and TextField is a no-op and recommended against by the Django & DRF docs

Added by dalley over 4 years ago. Updated almost 2 years ago.

Status:
MODIFIED
Priority:
Normal
Assignee:
-
Sprint/Milestone:
-
Start date:
Due date:
Estimated time:
Severity:
2. Medium
Version - Python:
Platform Release:
3.0.0
Target Release - Python:
OS:
Triaged:
Yes
Groomed:
No
Sprint Candidate:
No
Tags:
Sprint:
Quarter:

Description

Using null=True with CharField and TextField is recommended against and is also essentially a no-op. blank=True captures what I think we actually want.

https://docs.djangoproject.com/en/2.0/ref/models/fields/#null

If True, Django will store empty values as NULL in the database. Default is False.

Avoid using null on string-based fields such as CharField and TextField. If a string-based field has null=True, that means it has two possible values for “no data”: NULL, and the empty string. In most cases, it’s redundant to have two possible values for “no data;” the Django convention is to use the empty string, not NULL. One exception is when a CharField has both unique=True and blank=True set. In this situation, null=True is required to avoid unique constraint violations when saving multiple objects with blank values.

For both string-based and non-string-based fields, you will also need to set blank=True if you wish to permit empty values in forms, as the null parameter only affects database storage (see blank).

https://docs.djangoproject.com/en/2.0/ref/models/fields/#blank

If True, the field is allowed to be blank. Default is False.

Note that this is different than null. null is purely database-related, whereas blank is validation-related. If a field has blank=True, form validation will allow entry of an empty value. If a field has blank=False, the field will be required.

Actions #1

Updated by dalley over 4 years ago

  • Description updated (diff)
Actions #2

Updated by dalley over 4 years ago

  • Description updated (diff)
Actions #3

Updated by dalley over 4 years ago

  • Tags Easy Fix, Pulp 3 added
Actions #4

Updated by dalley over 4 years ago

  • Subject changed from We should be using blank=True instead of null=True with CharField and TextField to Using null=True with CharField and TextField is a no-op and recommended against by the Django & DRF docs
Actions #5

Updated by daviddavis over 4 years ago

Looks like there are some django lint tools we could maybe use. Specifically, this one seems to check for nullable Text/Char fields:

https://github.com/lamby/django-lint/blob/27e753656e0e9a60251a8c405920668a59f166de/DjangoLint/AstCheckers/model_fields.py#L32

Edit: Looks like this linter is python 2 only :(

Actions #6

Updated by dalley over 4 years ago

  • Triaged changed from No to Yes
  • Tags deleted (Easy Fix)

Added by dalley over 4 years ago

Revision 25932c96

Don't use null=True with TextField

closes #3446 https://pulp.plan.io/issues/3446

Actions #7

Updated by dalley over 4 years ago

  • Status changed from NEW to MODIFIED
Actions #8

Updated by bmbouter over 3 years ago

  • Tags deleted (Pulp 3)
Actions #9

Updated by dalley almost 2 years ago

  • Platform Release set to 3.0.0

Also available in: Atom PDF