Fields Reference
This page documents all localized field types provided by Django i18n Fields.
LocalizedField
Base class for all localized fields.
- class LocalizedField(**options)
A field that stores localized values in multiple languages using JSONField.
Parameters:
required(bool | list[str] | None): Languages that require a value.Nonewithblank=True: No languages requiredNonewithblank=False: Primary language (LANGUAGE_CODE) requiredTrue: All configured languages requiredFalse: No languages requiredList[str]: Specific languages required (e.g.,['en', 'es'])
Standard Django field options:
blank,null,default, etc.
Example:
from i18n_fields import LocalizedField class MyModel(models.Model): # Require English field1 = LocalizedField(required=['en']) # Require all languages field2 = LocalizedField(required=True) # No languages required field3 = LocalizedField(blank=True)
Methods:
- from_db_value(value, expression, connection)
Converts database value to LocalizedValue instance.
- to_python(value)
Converts value to LocalizedValue instance.
- get_prep_value(value)
Prepares LocalizedValue for database storage.
LocalizedCharField
- class LocalizedCharField(max_length, **options)
Localized version of Django’s
CharField.Parameters:
max_length(int): Maximum length for each translationAll
LocalizedFieldoptions
Example:
from i18n_fields import LocalizedCharField class Article(models.Model): title = LocalizedCharField( max_length=200, required=['en'], help_text="Article title in multiple languages" )
Value Type: Returns
LocalizedStringValueStorage: JSON with empty string as default for missing translations
LocalizedTextField
- class LocalizedTextField(**options)
Localized version of Django’s
TextField.Parameters:
All
LocalizedFieldoptions
Example:
from i18n_fields import LocalizedTextField class Article(models.Model): content = LocalizedTextField( blank=True, help_text="Article content" ) description = LocalizedTextField( required=['en', 'es'] )
Value Type: Returns
LocalizedStringValueUsage:
article = Article.objects.create( content={ 'en': 'Long content in English...', 'es': 'Contenido largo en español...' } ) print(article.content) # Returns translated content
LocalizedIntegerField
- class LocalizedIntegerField(**options)
Localized version of Django’s
IntegerField.Parameters:
All
LocalizedFieldoptions
Example:
from i18n_fields import LocalizedIntegerField class Product(models.Model): stock = LocalizedIntegerField( blank=True, help_text="Stock quantity by region" )
Value Type: Returns
LocalizedIntegerValueUsage:
product = Product.objects.create( stock={ 'en': 100, # US stock 'es': 50, # Spain stock 'fr': 75 # France stock } ) # Get current language value print(product.stock) # Returns integer # Get specific language print(product.stock.get('es')) # Returns 50
LocalizedFloatField
- class LocalizedFloatField(**options)
Localized version of Django’s
FloatField.Parameters:
All
LocalizedFieldoptions
Example:
from i18n_fields import LocalizedFloatField class Product(models.Model): price = LocalizedFloatField( blank=True, help_text="Price in local currency" ) rating = LocalizedFloatField( null=True, blank=True )
Value Type: Returns
LocalizedFloatValueUsage:
product = Product.objects.create( price={ 'en': 99.99, # USD 'es': 89.99, # EUR 'jp': 10999.0 # JPY } ) with translation.override('es'): print(product.price) # Returns 89.99
LocalizedBooleanField
- class LocalizedBooleanField(**options)
Localized version of Django’s
BooleanField.Parameters:
All
LocalizedFieldoptions
Example:
from i18n_fields import LocalizedBooleanField class Feature(models.Model): is_enabled = LocalizedBooleanField( blank=True, help_text="Feature enabled by region" )
Value Type: Returns
LocalizedBooleanValueUsage:
feature = Feature.objects.create( is_enabled={ 'en': True, # Enabled in US 'es': False, # Disabled in Spain 'fr': True # Enabled in France } ) with translation.override('es'): if feature.is_enabled: print("Feature is enabled")
LocalizedFileField
- class LocalizedFileField(upload_to, **options)
Localized version of Django’s
FileField.Parameters:
upload_to(str | callable): Upload pathAll
LocalizedFieldoptions
Example:
from i18n_fields import LocalizedFileField class Document(models.Model): file = LocalizedFileField( upload_to='documents/%Y/%m/', blank=True, help_text="Document file in multiple languages" )
Value Type: Returns
LocalizedFileValueUsage:
# Upload files document = Document.objects.create( file={ 'en': english_file, 'es': spanish_file, 'fr': french_file } ) # Access file in current language with translation.override('es'): url = document.file.url name = document.file.name size = document.file.size
LocalizedMartorField
- class LocalizedMartorField(**options)
Localized field for Markdown text using the martor editor.
Requires the
martorpackage to be installed. Install viapip install django-i18n-fields[md]orpip install martor.Parameters:
All
LocalizedFieldoptions
Example:
from i18n_fields import LocalizedMartorField class Article(models.Model): content = LocalizedMartorField(blank=True) notes = LocalizedMartorField(required=['en'])
Value Type: Returns
LocalizedStringValueAdmin Integration:
When used with
LocalizedFieldsAdminMixinorLocalizedFieldsAdmin, the field automatically renders as a Markdown editor with preview support for each language tab/dropdown.Usage:
article = Article.objects.create( content={ 'en': '# Hello World\n\nThis is **markdown** content.', 'es': '# Hola Mundo\n\nEste es contenido en **markdown**.', } ) # Access like any other localized field print(article.content) # Returns markdown string in active language
Setup:
Install martor:
pip install django-i18n-fields[md]Add
'martor'toINSTALLED_APPSInclude martor URLs:
path('martor/', include('martor.urls'))
See the martor documentation for full configuration options (toolbar customization, image uploads, emoji support, etc.).
LocalizedUniqueSlugField
- class LocalizedUniqueSlugField(populate_from, **options)
Localized slug field with automatic generation and per-language uniqueness.
Parameters:
populate_from(str | callable): Field name or callable to generate slug fromAll
LocalizedFieldoptions
Example:
from i18n_fields import LocalizedUniqueSlugField, AtomicSlugRetryMixin class Article(models.Model, AtomicSlugRetryMixin): title = LocalizedCharField(max_length=200, required=['en']) slug = LocalizedUniqueSlugField( populate_from='title', unique=True )
Value Type: Returns
LocalizedStringValueFeatures:
Automatic slug generation from
populate_fromfieldPer-language uniqueness
Automatic retry with incrementing numbers on conflicts
Uses
MAX_RETRIESsetting for retry limit
Usage:
# Slug generated automatically article = Article.objects.create( title={ 'en': 'Hello World', 'es': 'Hola Mundo' } ) # Slugs: {'en': 'hello-world', 'es': 'hola-mundo'} print(article.slug.en) # 'hello-world' print(article.slug.es) # 'hola-mundo' # Conflict handling article2 = Article.objects.create( title={'en': 'Hello World'} ) # Slug: {'en': 'hello-world-2'}
With Mixin:
Use
AtomicSlugRetryMixinfor atomic slug generation:from i18n_fields import AtomicSlugRetryMixin class Article(models.Model, AtomicSlugRetryMixin): slug = LocalizedUniqueSlugField(populate_from='title')
Field Options
Common Options
All localized fields accept standard Django field options:
null(bool): IfTrue, empty values are stored as NULLblank(bool): IfTrue, field is not required in formsdefault: Default value for the fieldhelp_text(str): Help text for formsverbose_name(str): Human-readable namedb_column(str): Database column namedb_index(bool): Create database indexeditable(bool): Show in forms/admin
Required Option
The required parameter is specific to localized fields:
# No languages required
field = LocalizedCharField(blank=True)
# Primary language required
field = LocalizedCharField(required=['en'])
# Multiple languages required
field = LocalizedCharField(required=['en', 'es', 'fr'])
# All configured languages required
field = LocalizedCharField(required=True)
Validation
Required Language Validation
Localized fields validate that required languages have values:
class Article(models.Model):
title = LocalizedCharField(required=['en', 'es'])
# This will raise IntegrityError
article = Article.objects.create(
title={'en': 'Hello'} # Missing required 'es'
)
Null Validation
When null=False, at least one language must have a value:
class Article(models.Model):
title = LocalizedCharField(null=False, blank=True)
# This is OK - has at least one value
article = Article.objects.create(
title={'en': 'Hello'}
)
# This will raise error - all values are None
article = Article.objects.create(
title={'en': None, 'es': None}
)
Migration Support
All localized fields support Django migrations:
# Generated migration
operations = [
migrations.AddField(
model_name='article',
name='title',
field=i18n_fields.fields.LocalizedCharField(
max_length=200,
required=['en']
),
),
]
Best Practices
Choose Appropriate Field Type
Use the field type that matches your data:
# Text content title = LocalizedCharField(max_length=200) content = LocalizedTextField() # Numbers price = LocalizedFloatField() stock = LocalizedIntegerField() # Booleans is_active = LocalizedBooleanField()
Set Required Languages Carefully
Only require languages you can guarantee:
# Good - only require primary language title = LocalizedCharField(required=['en']) # Be careful - requires multiple languages title = LocalizedCharField(required=['en', 'es', 'fr'])
Use Appropriate Defaults
Set defaults for optional fields:
rating = LocalizedFloatField(blank=True, default=0.0) is_featured = LocalizedBooleanField(default=False)
Consider null vs blank
Use
blank=Truefor optional fields in formsUse
null=Trueto allow NULL in database
# Optional in forms, empty string in DB description = LocalizedTextField(blank=True) # Optional in forms, NULL in DB notes = LocalizedTextField(blank=True, null=True)
See Also
Values Reference - LocalizedValue classes
Basic Usage - Basic usage examples
Django Admin Integration - Admin integration