Files
Django-Translatable-Fields/example/models.py
Holger Sielaff 79c68169f6 Initial
2025-08-02 20:08:33 +02:00

91 lines
3.8 KiB
Python

"""
Example models demonstrating django-translatable-fields usage.
"""
from django.db import models
from django_translatable_fields.fields import CharField, TextField, EmailField, URLField, SlugField
from django_translatable_fields.descriptors import TranslatableModelMixin
class Product(TranslatableModelMixin, models.Model):
"""
Example product model with translatable fields.
"""
name = CharField(max_length=200, translatable=True, help_text="Product name")
description = TextField(translatable=True, help_text="Product description")
slug = SlugField(max_length=200, translatable=True, help_text="SEO-friendly URL slug")
support_email = EmailField(translatable=True, help_text="Support contact email")
website = URLField(translatable=True, blank=True, help_text="Product website URL")
price = models.DecimalField(max_digits=10, decimal_places=2)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
app_label = 'example'
def __str__(self):
return self.name or f"Product {self.id}"
class Category(TranslatableModelMixin, models.Model):
"""
Example category model with translatable fields.
"""
name = CharField(max_length=100, translatable=True, help_text="Category name")
description = TextField(translatable=True, blank=True, help_text="Category description")
slug = SlugField(max_length=100, translatable=True, help_text="URL-friendly category slug")
meta_description = CharField(max_length=160, translatable=True, blank=True, help_text="SEO meta description")
class Meta:
app_label = 'example'
verbose_name_plural = "Categories"
def __str__(self):
return self.name or f"Category {self.id}"
class BlogPost(TranslatableModelMixin, models.Model):
"""
Example blog post model demonstrating all translatable field types.
"""
title = CharField(max_length=200, translatable=True, help_text="Blog post title")
content = TextField(translatable=True, help_text="Main blog post content")
slug = SlugField(max_length=200, translatable=True, help_text="URL slug for the post")
author_email = EmailField(translatable=True, help_text="Author contact email")
external_link = URLField(translatable=True, blank=True, help_text="External reference URL")
meta_description = CharField(max_length=160, translatable=True, blank=True, help_text="SEO meta description")
# Non-translatable fields
published = models.BooleanField(default=False)
publish_date = models.DateTimeField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
app_label = 'example'
ordering = ['-created_at']
def __str__(self):
return self.title or f"Blog Post {self.id}"
class Company(TranslatableModelMixin, models.Model):
"""
Example company model with contact information in multiple languages.
"""
name = CharField(max_length=200, translatable=True, help_text="Company name")
description = TextField(translatable=True, help_text="Company description")
slug = SlugField(max_length=200, translatable=True, help_text="Company URL slug")
contact_email = EmailField(translatable=True, help_text="Main contact email")
website = URLField(translatable=True, help_text="Company website")
address = TextField(translatable=True, help_text="Company address")
# Non-translatable fields
founded_year = models.IntegerField(null=True, blank=True)
employee_count = models.IntegerField(null=True, blank=True)
is_active = models.BooleanField(default=True)
class Meta:
app_label = 'example'
verbose_name_plural = "Companies"
def __str__(self):
return self.name or f"Company {self.id}"