views:

1169

answers:

4

When you have a model field with a choices option you tend to have some magic values associated with human readable names. Is there in Django a convenient way to set these fields by the human readable name instead of the value?

Consider this model:

class Thing(models.Model):
  PRIORITIES = (
    (0, 'Low'),
    (1, 'Normal'),
    (2, 'High'),
  )

  priority = models.IntegerField(default=0, choices=PRIORITIES)

At some point we have a Thing instance and we want to set its priority. Obviously you could do,

thing.priority = 1

But that forces you to memorize the Value-Name mapping of PRIORITIES. This doesn't work:

thing.priority = 'Normal' # Throws ValueError on .save()

Currently I have this silly workaround:

thing.priority = dict((key,value) for (value,key) in Thing.PRIORITIES)['Normal']

but that's clunky. Given how common this scenario could be I was wondering if anyone had a better solution. Is there some field method for setting fields by choice name which I totally overlooked?

A: 

Simply replace your numbers with the human readable values you would like. As such:

PRIORITIES = (
('LOW', 'Low'),
('NORMAL', 'Normal'),
('HIGH', 'High'),
)

This makes it human readable, however, you'd have to define your own ordering.

AlbertoPL
+2  A: 

I'd probably set up the reverse-lookup dict once and for all, but if I hadn't I'd just use:

thing.priority = next(value for value, name in Thing.PRIORITIES
                      if name=='Normal')

which seems simpler than building the dict on the fly just to toss it away again;-).

Alex Martelli
Yes, tossing the dict is a little silly, now that you say it. :)
Alexander Ljungberg
+8  A: 

Do as seen here. Then you can use a word that represents the proper integer.

Like so:

LOW = 0
NORMAL = 1
HIGH = 2
STATUS_CHOICES = (
    (LOW, 'Low'),
    (NORMAL, 'Normal'),
    (HIGH, 'High'),
)

Then they are still integers in the DB.

Usage would be thing.priority = Thing.NORMAL

jonwd7
That's a nicely detailed blog posting on the subject. Hard to find with Google too so thanks.
Alexander Ljungberg
+1  A: 

Here's a field type I wrote a few minutes ago that I think does what you want. Its constructor requires an argument 'choices', which may be either a tuple of 2-tuples in the same format as the choices option to IntegerField, or instead a simple list of names (ie ChoiceField(('Low', 'Normal', 'High'), default='Low') ). The class takes care of the mapping from string to int for you, you never see the int.

  class ChoiceField(models.IntegerField):
    def __init__(self, choices, **kwargs):
     if not hasattr(choices[0],'__iter__'):
      choices = zip(range(len(choices)), choices)

     self.val2choice = dict(choices)
     self.choice2val = dict((v,k) for k,v in choices)

     kwargs['choices'] = choices
     super(models.IntegerField, self).__init__(**kwargs)

    def to_python(self, value):
     return self.val2choice[value]

    def get_db_prep_value(self, choice):
     return self.choice2val[choice]
That's not bad Allan. Thanks!
Alexander Ljungberg