Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
django Multilanguage:按翻译字段的订单下拉字段_Django_Django Forms_Django Multilingual - Fatal编程技术网

django Multilanguage:按翻译字段的订单下拉字段

django Multilanguage:按翻译字段的订单下拉字段,django,django-forms,django-multilingual,Django,Django Forms,Django Multilingual,如何按已翻译字段排序表单字段的选项 models.py: class UserProfile(models.Model): ... country=models.ForeignKey('Country') class Country(models.Model): class Translation(multilingual.Translation): name = models.CharField(max_length=60) ... temp

如何按已翻译字段排序表单字段的选项

models.py:

class UserProfile(models.Model):
    ...
    country=models.ForeignKey('Country')

class Country(models.Model):
    class Translation(multilingual.Translation):
        name = models.CharField(max_length=60)
    ...
template.html:

{# userprofileform is a standard modelform for UserProfile #}
{{ userprofileform.country }}
多谢各位

编辑:

我希望
select
字段的选项根据语言按名称或名称排序:

<!-- English -->
<select>
    <option>Afganistan</option>
    <option>Austria</option>
    <option>Bahamas</option>
</select>

<!-- German (as it is) -->
<select>
    <option>Afganistan</option>
    <option>Österreich</option>
    <option>Bahamas</option>
</select>

<!-- German (as it should be) -->
<select>
    <option>Afganistan</option>
    <option>Bahamaas</option>
    <option>Österreich</option>
</select>

阿富汗
奥地利
巴哈马
阿富汗
Österreich
巴哈马
阿富汗
巴哈马群岛
Österreich

我对i18n没有任何实际的经验,因此我不知道您在后端可以使用什么,但是您可以使用javascript对浏览器中的菜单进行排序

您可以尝试在表单中使用自定义小部件,以便排序在django进行翻译之前进行。也许这段来自我当前项目的片段能有所帮助

import locale
from django_countries.countries import COUNTRIES
from django.forms import Select, Form, ChoiceField

class CountryWidget(Select):

    def render_options(self, *args, **kwargs):
        # this is the meat, the choices list is sorted depending on the forced 
        # translation of the full country name. self.choices (i.e. COUNTRIES) 
        # looks like this [('DE':_("Germany")),('AT', _("Austria")), ..]
        # sorting in-place might be not the best idea but it works fine for me
        self.choices.sort(cmp=lambda e1, e2: locale.strcoll(unicode(e1[1]),
           unicode(e2[1])))
        return super(CountryWidget, self).render_options(*args, **kwargs)

class AddressForm(Form):
    sender_country = ChoiceField(COUNTRIES, widget=CountryWidget, initial='DE')

我通过动态加载选择值解决了类似的问题。一点也不觉得脏

从包含国家名称的名称字段的国家模型中获取值的示例。使用相同的逻辑,您可以从任何地方获取您的值

from django.utils.translation import ugettext_lazy as _
from mysite.Models import Country

class UserProfileForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(UserProfileForm, self).__init__(*args, **kwargs)
        self.fields['country'].queryset = Country.objects.order_by(_('name'))

    class Meta:
        model = Country

你能说得更具体些吗?你说的“一个字段一个字段排序”到底是什么意思?也许举个例子会有所帮助。