我们如何在view.py中使用fiter来获取hvad中的multiilngual模型

时间:2014-09-17 07:54:01

标签: django-views django-multilingual

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

models.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),      
)

IN views.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 获取一个。

我经常搜索但找不到解决方案,请帮助!! 在此先感谢!!

1 个答案:

答案 0 :(得分:0)

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

我使用了这样的queryset:

if (searchString != null) { //PageIndex has to be reset, if searchstring is changed during paging (user not on first page) //This avoids "Object reference not set to an instance of an object" when //on page 1,2,3.... etc currentPageIndex = 0; PageIndex = currentPageIndex; }

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