On Wednesday, April 4, 2012 9:41:23 AM UTC-7, Adrian Holovaty wrote: > 2012/4/3 Łukasz Langa <luk...@langa.pl>: > > Explicit choice values:: > > > > GENDER_MALE = 0 > > GENDER_FEMALE = 1 > > GENDER_NOT_SPECIFIED = 2 > > > > GENDER_CHOICES = ( > > (GENDER_MALE, _('male')), > > (GENDER_FEMALE, _('female')), > > (GENDER_NOT_SPECIFIED, _('not specified')), > > ) > > > > class User(models.Model): > > gender = models.IntegerField(choices=GENDER_CHOICES, > > default=GENDER_NOT_SPECIFIED) > > > > def greet(self): > > if self.gender == GENDER_MALE: > > return 'Hi, boy.' > > elif self.gender == GENDER_NOT_SPECIFIED: > > return 'Hello, girl.' > > else: return 'Hey there, user!' > > > > This is a saner way but starts getting overly verbose and redundant. You > can > > improve encapsulation by moving the choices into the ``User`` class but > that on > > the other hand beats reusability. > > I don't see the immediate need for Yet Another Sub-framework, as > described in this proposal. This is what I normally do, and it works > fine: > > class User(models.Model): > MALE = 0 > FEMALE = 1 > GENDERS = [(MALE, 'Male'), (FEMALE, 'Female')] > gender = models.IntegerField(choices=GENDERS) > > def greet(self): > return {MALE: 'Hi, boy', FEMALE: 'Hi, girl.'}[self.gender] > > If people aren't understanding that, we should improve our documentation. > > Also, the fact that we *don't* have a sub-framework for this lets > people do whatever they want -- including using the dj.choices module. > So I am -1 on including this in Django proper. > > Adrian >
<delurk> +1 to what Adrian says. In fact... On two occasions now I've had to do serious debugging on implementations done by other people who used a technique not dissimilar to what Łukasz proposes. In both cases, while the inheritance and better control structures were nice, the issue was that if you give people enough rope to hang themselves, they will. The current system works very well, especially when you follow Adrian's pattern. Lukasz' system works well too. Therefore... I submit that we keep Django core for choices as is and let people who want to use another system implement 3rd party packages. </delurk> -- You received this message because you are subscribed to the Google Groups "Django developers" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-developers/-/F20PJZowsZ4J. 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.