LanguageField

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from django.conf import settings

class LanguageField(models.CharField):
    def __init__(self, *args, **kwargs):
        kwargs.setdefault('maxlength', 5)
        kwargs.setdefault('choices', settings.LANGUAGES)
        
        super(LanguageField, self).__init__(*args, **kwargs)

    def get_internal_type(self):
        return "CharField"

More like this

  1. Admin list thumbnail by theetderks 6 years, 11 months ago
  2. Frequently used tags/filters for Jinja2 by mathwizard 5 years, 7 months ago
  3. Add example contact with BaseCommand by magik_cypress 1 year, 8 months ago
  4. Add example content with BaseCommand by magik_cypress 1 year, 8 months ago
  5. Remove scheme, host and port from url by twinsant 7 years, 1 month ago

Comments

aguafuertes (on July 9, 2009):

Thanks for the snippet.

A little typo: in Line 5 it must be 'max_length' instead of 'maxlength'

#

jeverling (on May 16, 2012):

Although this snippet is rather old, it still works fine... :) If you want to use I18N, you should use this:

from django.utils import translation
kwargs.setdefault('choices', [(k, translation.ugettext(v)) for k, v in settings.LANGUAGES])

Also, 5 chars is not enough anymore, for example sr-latn has 7.

#

(Forgotten your password?)