Login

LanguageField

Author:
marinho
Posted:
December 1, 2007
Language:
Python
Version:
.96
Score:
2 (after 4 ratings)

How to use: simple as a CharField.

 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. Browser-native date input field by kytta 1 month, 1 week ago
  2. Generate and render HTML Table by LLyaudet 1 month, 2 weeks ago
  3. My firs Snippets by GutemaG 1 month, 2 weeks ago
  4. FileField having auto upload_to path by junaidmgithub 2 months, 3 weeks ago
  5. LazyPrimaryKeyRelatedField by LLyaudet 3 months 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.

#

julius425 (on May 8, 2019):

Sir, this is still helpful. Many thanks.

#

Please login first before commenting.