#18212 closed Bug (fixed)
GenericIPAddressField does not handle verbose_name and name args like other field types
Reported by: | Dan McGee | Owned by: | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.4 |
Severity: | Release blocker | Keywords: | fields |
Cc: | net147 | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
Trying to create a field like this fails:
ip_verbose_name = models.GenericIPAddressField("IP Address Verbose", blank=True, null=True)
Adding a field definition like this to a test case stops runtests.py dead in its tracks:
File "/home/dmcgee/projects/django/django/contrib/comments/models.py", line 61, in Comment unpack_ipv4=True, blank=True, null=True) File "/home/dmcgee/projects/django/django/db/models/fields/__init__.py", line 1042, in __init__ validators.ip_address_validators(protocol, unpack_ipv4) File "/home/dmcgee/projects/django/django/core/validators.py", line 130, in ip_address_validators "You can only use `unpack_ipv4` if `protocol` is set to 'both'") ValueError: You can only use `unpack_ipv4` if `protocol` is set to 'both'
Attached is a fix along with added test cases to ensure all core field types treat verbose_name correctly.
Attachments (1)
Change History (8)
comment:1 by , 13 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
follow-up: 3 comment:2 by , 13 years ago
Resolution: | invalid |
---|---|
Status: | closed → reopened |
I didn't include every other field, but literally every other core field type takes verbose_name as a first argument, not a keyword argument, if it comes first. Note that they are also all documented the same way as the example you called out- none of them explicitly list verbose_name in the arguments list.
Please look at the test_field_verbose_name
method I added. In addition, I've attached a patch that tests *every single core field type*, and all of them take a keyword-less first parameter as verbose name. This is 23 field types in all.
Not only that, I can quote your own documentation which refutes your statement that "nothing in the current documentation guarantees that the first argument to any field constructor will be the verbose_name":
Verbose field names
Each field type, except for ForeignKey, ManyToManyField and OneToOneField, takes an optional first positional argument -- a verbose name. If the verbose name isn't given, Django will automatically create it using the field's attribute name, converting underscores to spaces.
Source: https://docs.djangoproject.com/en/1.4/topics/db/models/#verbose-field-names
by , 13 years ago
Attachment: | generic_ip_verbose_name.patch added |
---|
comment:3 by , 13 years ago
Owner: | removed |
---|---|
Severity: | Normal → Release blocker |
Status: | reopened → new |
Triage Stage: | Unreviewed → Accepted |
Replying to toofishes:
Source: https://docs.djangoproject.com/en/1.4/topics/db/models/#verbose-field-names
You are right, sorry. Now the problem to fix it is that we have a backward compatibility problem, for people who did define the protocol as first argument of GenericIPAddressField. As the field type has been added in 1.4, this could be fixed in 1.4.1 with a loud warning. Other opinions welcome.
comment:4 by , 13 years ago
Yeah, this needs to be fixed in 1.4.1 and should be noted as a minor backwards incompatibility in the release notes.
comment:5 by , 12 years ago
Cc: | added |
---|
comment:6 by , 12 years ago
Owner: | set to |
---|---|
Resolution: | → fixed |
Status: | new → closed |
Nothing in the current documentation guarantees that the first argument to any field constructor will be the verbose_name.
For example, GenericIPAddressField is documented as:
verbose_name should always be used as a keyword parameter, like most other field init parameters.