Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Proposal: upgrading the choices machinery for Django
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Thomas Guettler  
View profile  
 More options Apr 4 2012, 9:55 am
From: Thomas Guettler <h...@tbz-pariv.de>
Date: Wed, 04 Apr 2012 15:55:13 +0200
Local: Wed, Apr 4 2012 9:55 am
Subject: Re: Proposal: upgrading the choices machinery for Django
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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.