django模型中的字段和model名显示为中文小技巧分享

  

以下是“Django模型中的字段和model名显示为中文小技巧分享”的完整攻略:

step 1:安装django-modeltranslation

要实现将Django模型中的字段和model名显示为中文,我们需要借助django-modeltranslation这个第三方库。在安装之前,确保你的Django版本为1.8以上,且已经安装好了pip工具。

在终端运行以下命令,安装django-modeltranslation:

pip install django-modeltranslation

step 2:配置settings.py

接下来,需要在Django项目的settings.py文件中做出如下配置:

# 添加以下内容到settings.py文件底部

# 语言支持设置
LANGUAGES = (
    ('en', 'English'),
    ('zh-hans', '简体中文'),
)

# 使用缓存
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        'LOCATION': 'unique-snowflake'
    }
}

# 自动识别语言,也可以设置默认语言
LANGUAGE_CODE = 'zh-hans'
USE_LANGUAGES = True

# 指定模型翻译的语言
MODELTRANSLATION_DEFAULT_LANGUAGE = 'zh-hans'

step 3:为需要翻译的模型添加翻译字段

在Django模型中,我们可以为需要翻译的字段添加一个翻译字段。例如,以下是需要翻译的客户信息模型:

from django.db import models

class Customer(models.Model):
    name = models.CharField(max_length=100, verbose_name='姓名')
    mobile = models.CharField(max_length=11, verbose_name='手机号')
    address = models.CharField(max_length=200, verbose_name='地址')

为了实现中文翻译,我们可以在模型中为需要翻译的字段添加一个翻译字段。修改后的模型代码如下:

from django.db import models
from modeltranslation.utils import build_localized_fieldname

class Customer(models.Model):
    name = models.CharField(max_length=100, verbose_name='姓名')
    mobile = models.CharField(max_length=11, verbose_name='手机号')
    address = models.CharField(max_length=200, verbose_name='地址')

    # 为需要翻译的字段添加翻译字段
    name_zh = models.CharField(max_length=100, verbose_name=build_localized_fieldname('姓名', 'zh-hans'))
    mobile_zh = models.CharField(max_length=11, verbose_name=build_localized_fieldname('手机号', 'zh-hans'))
    address_zh = models.CharField(max_length=200, verbose_name=build_localized_fieldname('地址', 'zh-hans'))

step 4:注册翻译字段

为了让django-modeltranslation能够正确地识别需要翻译的模型和字段,我们需要在admin.py文件中注册这些字段。例如,以下是Customer模型对应的admin.py文件:

from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from modeltranslation.admin import TranslationAdmin
from .models import Customer

class CustomerAdmin(TranslationAdmin):
    list_display = ('name', 'mobile', 'address')

admin.site.register(Customer, CustomerAdmin)

其中,TranslationAdmin是django-modeltranslation提供的一个Model Admin类,它与Django的Model Admin类类似,但支持翻译字段。

示例说明1:翻译教师信息模型

以下是需要翻译的教师信息模型:

from django.db import models

class Teacher(models.Model):
    name = models.CharField(max_length=100, verbose_name='姓名')
    age = models.IntegerField(verbose_name='年龄')
    gender = models.CharField(max_length=2, choices=(('M', '男'), ('F', '女')), verbose_name='性别')
    subject = models.CharField(max_length=100, verbose_name='科目')

为了实现中文翻译,我们可以在模型中为需要翻译的字段添加一个翻译字段。修改后的模型代码如下:

from django.db import models
from modeltranslation.utils import build_localized_fieldname

class Teacher(models.Model):
    name = models.CharField(max_length=100, verbose_name='姓名')
    age = models.IntegerField(verbose_name='年龄')
    gender = models.CharField(max_length=2, choices=(('M', '男'), ('F', '女')), verbose_name='性别')
    subject = models.CharField(max_length=100, verbose_name='科目')

    # 为需要翻译的字段添加翻译字段
    name_zh = models.CharField(max_length=100, verbose_name=build_localized_fieldname('姓名', 'zh-hans'))
    age_zh = models.IntegerField(verbose_name=build_localized_fieldname('年龄', 'zh-hans'))
    gender_zh = models.CharField(max_length=2, choices=(('M', '男'), ('F', '女')), verbose_name=build_localized_fieldname('性别', 'zh-hans'))
    subject_zh = models.CharField(max_length=100, verbose_name=build_localized_fieldname('科目', 'zh-hans'))

然后,在admin.py文件中注册这些字段:

from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from modeltranslation.admin import TranslationAdmin
from .models import Teacher

class TeacherAdmin(TranslationAdmin):
    list_display = ('name', 'age', 'gender', 'subject')

admin.site.register(Teacher, TeacherAdmin)

示例说明2:翻译网站导航信息模型

以下是需要翻译的网站导航信息模型:

from django.db import models

class Nav(models.Model):
    name = models.CharField(max_length=100, verbose_name='名称')
    url = models.CharField(max_length=200, verbose_name='URL地址')
    logo = models.ImageField(upload_to='nav/logo/', verbose_name='LOGO')

同样地,我们需要在模型中为需要翻译的字段添加一个翻译字段:

from django.db import models
from modeltranslation.utils import build_localized_fieldname

class Nav(models.Model):
    name = models.CharField(max_length=100, verbose_name='名称')
    url = models.CharField(max_length=200, verbose_name='URL地址')
    logo = models.ImageField(upload_to='nav/logo/', verbose_name='LOGO')

    # 为需要翻译的字段添加翻译字段
    name_zh = models.CharField(max_length=100, verbose_name=build_localized_fieldname('名称', 'zh-hans'))
    url_zh = models.CharField(max_length=200, verbose_name=build_localized_fieldname('URL地址', 'zh-hans'))

admin.py文件中注册这些字段:

from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from modeltranslation.admin import TranslationAdmin
from .models import Nav

class NavAdmin(TranslationAdmin):
    list_display = ('name', 'url', 'logo')

admin.site.register(Nav, NavAdmin)

以上就是“Django模型中的字段和model名显示为中文小技巧分享”的完整攻略。希望对您有所帮助!

相关文章