1

我正在为应用程序使用多语言我开发的课程应用程序使用 hvad

模型.py

从 hvad.models 导入 TranslatableModel、TranslatedFields、TranslationManager

课程(TranslatableModel):

translations = TranslatedFields(
    domain = models.ForeignKey('domain.Domain'),
    #track = models.ForeignKey('track.Track'),
    track = models.ForeignKey('track.Track', blank=True, null=True),
    course_nm = models.CharField(max_length=100, verbose_name =_('Course Name')),
    nature_of_training = models.TextField(verbose_name = _('Nature of Training')),
    duration = models.PositiveIntegerField(verbose_name =_('Duration')),
    sem = models.PositiveIntegerField(verbose_name =_('Semester')),
    degree_level = models.CharField(max_length=100, verbose_name =_('Degree Level')),
    credit = models.PositiveIntegerField(verbose_name =_('Credits')),
    locations = models.CharField(max_length=100, verbose_name =_('Locations')),
    accessible = models.CharField(max_length=100, verbose_name =_('Accessible')),
    des = models.TextField(verbose_name = _('Description')),
    admission_details = models.TextField(verbose_name = _('Admission Details')),
    further_study_details = models.TextField(verbose_name = _('Further Study Details')),
    seats = models.PositiveIntegerField(verbose_name =_('Seats')),

    title = models.CharField(max_length=512, verbose_name=_('Title')),
    slug = models.SlugField(max_length=512, verbose_name=_('Slug')),
    created_date = models.DateTimeField(auto_now_add=True, blank=True, null=True),
    updated_date = models.DateTimeField(auto_now=True, blank=True, null=True),
    created_by = models.ForeignKey(User, blank=True, null=True, editable=False),      
)

在视图.py

def get_context_data(self, **kwargs):

    context = super(DegreeDetailView, self).get_context_data(**kwargs)

    context['courses'] = Course.objects.all().order_by('track')

    return context

当我在views.py中使用order_by时,它会给出以下错误

要访问未翻译模型中的“track”等已翻译字段,您必须使用翻译感知管理器,您可以使用 nani.utils.get_translation_aware_manager 获得一个

我搜索了很多但找不到解决方案,请帮助!提前致谢!!

4

1 回答 1

0

自从你问这个问题以来已经太晚了..但是如果有人像我一样遭受这种问题。

我像这样使用查询集:

Course.objects.untranslated().prefetch_related('translations').all().filter(translations__track__icontains='lorem ipsum')

我建议不要将 ForeignKeys 放在翻译中()

于 2017-06-09T08:47:53.987 回答