2010-01-26 3 views
2

어떻게 번역 필드로 양식 필드의 옵션을 주문합니까?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) 
    ... 

template.html :

{# userprofileform is a standard modelform for UserProfile #} 
{{ userprofileform.country }} 

당신을 감사

편집 :

나는 select 필드의 옵션이 원하는 name_de 또는 name_en으로 정렬 언어에 따라 :

<!-- 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> 
+1

더 구체적으로 말씀해 주시겠습니까? "다른 필드로 필드를 정렬"한다는 것은 정확히 무엇을 의미합니까? 예가 도움이 될 수 있습니다. – 3lectrologos

+0

희망적입니다. – tback

답변

0

나는 국제화와 실제 경험이없는, 그래서 백엔드에 당신에게 사용할 수 모르겠어요,하지만 당신은 브라우저

의 메뉴를 정렬하는 자바 스크립트를 사용할 수
+0

네,하지만 그렇게했다면 더러운 느낌이들 것입니다. – tback

1

양식에서 사용자 정의 위젯을 사용하여 장고가 번역을 수행하기 전에 정렬 작업을 시도해 볼 수 있습니다. 어쩌면 내 현재 프로젝트에서이 스 니펫을 도울 수 있습니다

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') 
1

선택 값을 동적으로로드하여 유사한 문제를 해결했습니다. 전혀 더러워지지 않았다.

국가 이름이 포함 된 국가 필드에서 값을 국가 모델에서 가져 오는 예제입니다. 동일한 논리를 사용하면 어디서나 가치를 얻을 수 있습니다.

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 
관련 문제