Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#20484 closed Bug (fixed)

GenericIPAddressField must not allow blank for NOT NULL fields

Reported by: erikr Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: eromijn@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

GenericIPAddressField internally translates all blank values, e.g. an empty string, to None/NULL before saving them in the database. This is good, because in PostgreSQL this field is backed by INET, meaning an empty string is not allowed (this problem also affects IPAddressField, but we can't change it as it would break backwards compatibility, #5622).

However, this means that a field which allows blank but not null values, will trigger a database error at runtime when trying to save a blank value. Therefore, the model validation should not allow GenericIPAddressFields to have blank but not null.

To summarise:

  • Current behaviour: when storing a blank value in a GenericIPAddressField(blank=True, null=False), a database exception will be triggered
  • New behaviour: a GenericIPAddressField(blank=True, null=False) will trigger a model validation error

Change History (7)

comment:1 Changed 2 years ago by erikr

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 2 years ago by mjtamlyn

  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 2 years ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from new to closed

In ded95ccdce0ba983c405ddde9eb071e454245a94:

Fixed #20484 -- Added model validation for GenericIPAddressField

GenericIPAddressField must not allow blank for NOT NULL fields

Thanks Erik Romijn.

comment:4 Changed 2 years ago by chrismedrela

This patch includes no doc in docs/ref/models/fields.txt. I've created a pull request with a note in the file: https://github.com/django/django/pull/1453.

comment:5 Changed 2 years ago by Christopher Medrela <chris.medrela@…>

In fb26c4996a0c4d41aa80d28ce65ab050ffe6df6b:

Fixed #20484 again -- added note to field documentation

comment:6 Changed 2 years ago by Preston Holmes <preston@…>

In cf041b883c6a089f0b113846918fdb074e465713:

Merge pull request #1453 from chrismedrela/ticket20484-improvement

Fixed #20484 again -- added note to field documentation

comment:7 Changed 2 years ago by Tim Graham <timograham@…>

In 6264e39c346a1b563dc0cbc31c5fb75830e032cb:

[1.6.x] Added note to GenericIPAddressField documentation

refs #20484

Backport of fb26c4996a from master

Note: See TracTickets for help on using tickets.
Back to Top