2012-05-16 31 views
1

我有如下文件的國家的名單,顯示,從2維元組下拉列表

COUNTRIES = (
    ('AF', 'Afghanistan'), 
    ('AX', '\xc5land Islands'), 
    ('AL', 'Albania'),... 

我想在我的下拉列表中只顯示各國的全名及保存只有模型中的全名。

現在我的models.py是

class UserProfiles(models.Model): 
    location = models.CharField(max_length=60, blank=True, null=True) 

和forms.py是

class UserProfileForm(ModelForm): 
    location = forms.ChoiceField(choices=COUNTRIES) 
+0

如果我正確地得到您的問題,這不工作? COUNTRIES =( ( '阿富汗', '阿富汗'), ( '\ xc5land列島','\ xc5land羣島), ( '阿爾巴尼亞', '阿爾巴尼亞'),... – zubinmehta

+0

這不是解決方案,但UserProfileForm是沒有必要的。你可以有這樣的事情:class UserProfiles(models.Model): location = models.CharField(max_length = 60,choices = COUNTRIES,blank = True,null = True) – Goin

回答

0

調整國家列出這樣

COUNTRIES = [ (long, long) for short, long in COUNTRIES ] 

然後在模型中

使用
class UserProfiles(models.Model): 
    location = models.CharField(max_length=60, blank=True, null=True, choices=COUNTRIES) 

該表格應該是好的,因爲它是

+0

should not它是'COUNTRIES = [國家(長,短)在國家]' – John

+0

@約翰no。你需要一個2元組的CHOICES列表,即使每個元組中的兩個條目都是相同的 –

+0

好吧,我把那個line.py和forms.py文件,但是當我使用管理員添加一個新的配置文件。位置字段爲空。沒有列表或文本框! – John