Am 03.04.2012 21:31, schrieb Łukasz Langa:
A nice HTML rendering of this proposal is available at:
http://lukasz.langa.pl/2/upgrading-choices-machinery-django/
==========================================
Upgrading the choices machinery for Django
==========================================
...
since most people have something like this in their code, a better solution
should require
only some additional characters.
GENDER_CHOICES = (
(0, 'male'),
(1, 'female'),
(2, 'not specified'),
)
# if this would be supported, updating the code would be easy
GENDER_CHOICES = Choices(
(0, 'male'),
(1, 'female'),
(2, 'not specified'),
)
#about Group
>>> class License(Choices):
... COPYLEFT = Choices.Group(0)
... gpl_any = Choice("GPL, any")
I don't like Choices.Group. I think it is better if choices must be set
explicit, not
implicit (auto-increment).
Here is my solution, which is similar to way3 in your proposal.
{{{
class ChoiceDict(SortedDict):
'''
Iterating this SortedDict will return
the items (and not the keys). This is handy if
you want to uses this for a choice list on
a model field
'''
__iter__=SortedDict.iteritems
class Foo(models.Model):
STATE_NEW='new'
STATE_DONE='done'
states=ChoiceDict((STATE_NEW, _('new')),
(STATE_DONE, _('done')),
)
state=models.CharField(max_length=16, verbose_name=u'Status',
default=STATE_NEU, choices=states)
}}}
I like this since, you can access the verbose string like this:
Foo.states[obj.state]
--
Thomas Guettler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de
--
You received this message because you are subscribed to the Google Groups "Django
developers" group.
To post to this group, send email to django-developers@googlegroups.com.
To unsubscribe from this group, send email to
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/django-developers?hl=en.