Django Translatable Fields - Examples
This directory contains comprehensive examples demonstrating all features of django-translatable-fields.
Files Overview
models.py- Example models showcasing all translatable field typesadmin.py- Admin configurations for the example modelsusage_examples.py- Practical usage examples and demonstrations
Example Models
1. Product Model
Demonstrates basic translatable fields:
CharField- Product nameTextField- Product descriptionSlugField- SEO-friendly URL slugEmailField- Support contact emailURLField- Product website URL
2. Category Model
Shows category organization with translations:
CharField- Category nameTextField- Category descriptionSlugField- URL-friendly category slugCharField- SEO meta description
3. BlogPost Model
Comprehensive example with all field types:
CharField- Blog post title and meta descriptionTextField- Main blog post contentSlugField- URL slug for the postEmailField- Author contact emailURLField- External reference URL
4. Company Model
Business-focused model with contact information:
CharField- Company nameTextField- Company description and addressSlugField- Company URL slugEmailField- Main contact emailURLField- Company website
Running the Examples
1. Django Shell Usage
# Start Django shell
python manage.py shell
# Import and run examples
from example.usage_examples import run_all_examples
run_all_examples()
2. Individual Example Functions
from example.usage_examples import (
create_sample_data,
demonstrate_language_switching,
demonstrate_direct_access,
demonstrate_translation_methods,
demonstrate_all_field_types
)
# Create sample data
product, blog_post, company = create_sample_data()
# Try different demonstrations
demonstrate_language_switching()
demonstrate_direct_access()
demonstrate_translation_methods()
demonstrate_all_field_types()
Usage Patterns Demonstrated
1. Language-Aware Access
from django.utils.translation import activate
# Fields automatically return values based on current language
activate('en')
print(product.name) # "Awesome Widget"
activate('de')
print(product.name) # "Fantastisches Widget"
2. Direct Language Access
# Access specific language directly
print(product.name_en) # "Awesome Widget"
print(product.name_de) # "Fantastisches Widget"
print(product.name_fr) # "Widget Fantastique"
3. Translation Management
# Get all translations for a field
translations = product.get_all_translations('name')
# {'en': 'Awesome Widget', 'de': 'Fantastisches Widget', 'fr': 'Widget Fantastique'}
# Get specific translation
german_name = product.get_translation('name', 'de')
# Set translation programmatically
product.set_translation('name', 'es', 'Widget Fantástico')
product.save()
4. All Field Types in Action
The examples demonstrate proper usage of:
- CharField - Text fields with max_length
- TextField - Longer text content
- SlugField - URL-friendly identifiers
- EmailField - Email addresses with validation
- URLField - Web URLs with validation
Admin Interface Features
The admin examples show:
- Clean interface with translate buttons
- Modal overlays for translation management
- WYSIWYG editor for TextField content
- Proper field validation per language
- Drag and resize functionality for modals
Field Validation
Each field type includes proper validation:
- EmailField - Valid email format per language
- URLField - Valid URL format per language
- SlugField - Slug format validation per language
- CharField/TextField - Length and content validation
Integration with Django Features
Examples demonstrate compatibility with:
- Django admin interface
- Model forms and validation
- Django's internationalization system
- Search functionality
- List filters and display options
- Prepopulated fields (for slugs)
Best Practices Shown
- Model Design - Proper use of translatable vs non-translatable fields
- Admin Configuration - Clean, user-friendly admin interfaces
- Data Access - Efficient translation retrieval and management
- Validation - Proper field validation across languages
- SEO Optimization - Language-specific slugs and meta descriptions
Testing Your Implementation
Use these examples to:
- Verify all field types work correctly
- Test language switching functionality
- Validate admin interface behavior
- Check translation storage and retrieval
- Ensure proper field validation
The examples provide a solid foundation for implementing translatable fields in your own Django applications.