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
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 8 months ago
Comments
Thanks for the snippet.
A little typo: in Line 5 it must be 'max_length' instead of 'maxlength'
#
Although this snippet is rather old, it still works fine... :) If you want to use I18N, you should use this:
Also, 5 chars is not enough anymore, for example sr-latn has 7.
#
Sir, this is still helpful. Many thanks.
#
Please login first before commenting.