#16663: Model field choices should accept empty iterator
------------------------+----------------------------------------------
Reporter: danielnaab | Owner: nobody
Type: Bug | Status: new
Milestone: | Component: Database layer (models, ORM)
Version: 1.3 | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Easy pickings: 1
UI/UX: 0 |
------------------------+----------------------------------------------
The base `Field` class constructor uses the following shorthand to
initialize the `choices` for a field (line 96 of
`django/db/model/fields/__init__.py`):
{{{
self._choices = choices or []
}}}
In Python, an empty iterator evaluates to False, so if we did:
{{{
class MyModel(models.Model):
my_field = models.IntegerField(choices=my_list_like_object)
}}}
`_choices` will default to an empty list if the `my_list_like_object` is
permanently or temporarilly empty at time of class definition. However,
the documentation clearly states the intent of allowing
[https://docs.djangoproject.com/en/dev/ref/models/fields/#choices any
iterable] as a `choices` kwarg:
Finally, note that choices can be any iterable object -- not
necessarily a list or tuple. This lets you construct choices dynamically.
But if you find yourself hacking choices to be dynamic, you're probably
better off using a proper database table with a ForeignKey. choices is
meant for static data that doesn't change much, if ever.
So I propose making a small change to the above line:
{{{
if choices == None:
self._choices = []
else:
self._choices = choices
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/16663>
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 post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.