Translated choices fields

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from django.db import models
from django.utils.translation import ugettext_lazy as _

def make_choices(choices):
    """
    Returns tuples of localized choices based on the dict choices parameter.
    Uses lazy translation for choices names.
    """
    return tuple([(k, _(v)) for k, v in choices.items()])

class Quality(models.Model):
    QUALITY_CHOICES = {
        1: 'bad',
        2: 'regular',
        3: 'good',
    }
    q = models.PositiveSmallIntegerField(choices=make_choices(QUALITY_CHOICES))
    class Admin: pass

Comments

(Forgotten your password?)

You may use Markdown syntax here, but raw HTML will be removed.