django-modeltranslation从模型

时间:2017-07-25 10:44:53

标签: python django django-modeltranslation

这是我的 translation.py 文件:

from modeltranslation.translator import translator, TranslationOptions
from polls.models import Question, Choice

class QuestionTranlationOptions(TranslationOptions):
    fields = ('question_text',)

class ChoiceTranslationOptions(TranslationOptions):
    fields = ('choice_text',)

translator.register(Question, QuestionTranlationOptions)
translator.register(Choice, ChoiceTranslationOptions);

models.py

from django.db import models
from django.utils import timezone
import datetime
from django.utils.translation import ugettext_lazy as _

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField(_('date published'))
    def __str__(self):
        return self.question_text
    def was_published_recently(self):
        now = timezone.now()
        return now - datetime.timedelta(days=1) <= self.pub_date <= now
    was_published_recently.admin_order_field = 'pub_date'
    was_published_recently.boolean = True
    was_published_recently.short_description = _('Published recently?')

class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
    def __str__(self):
        return self.choice_text

当我打开应用程序时,模型&#39;文字不可见。其中一个问题是&#34; Coke或Sprite?&#34;,但我看不到文字。我做错了什么?

  

Python 3.4,   Django 1.10

1 个答案:

答案 0 :(得分:0)

我必须运行此命令来修复它:

C:\Users\xxx\myproject>manage.py update_translation_fields