1

我有一堆字段的模型。其中两个领域有选择。他们是这样的:Django - 选择字段显示键,而不是值

SNAIL_MAIL_INVOICE_CHOICES = (
    ('Y', 'Yes'), 
    ('N', 'No'), 
) 

SNAIL_MAIL_CHOICES = (
    ('Y', 'Yes'), 
    ('N', 'No'), 
) 

snailMailOnly = models.CharField(max_length = 3, verbose_name = 'Snail Mail Only?', choices = SNAIL_MAIL_CHOICES, default='Y') 
snailMailInvoice = models.CharField(max_length = 3, verbose_name = 'Snail Mail Invoice?', choices = SNAIL_MAIL_INVOICE_CHOICES, default='Y') 

当我表现出Django模板这两个值,我这样做是这样的:

<tr><td>Snail Mail Only?</td><td>{{contact.get_snailMailOnly_display}}</td></tr> 
    <tr><td>Snail Mail Invoice?</td><td>{{contact.get_snailMailInvoice_display}}</td></tr> 

的问题是,虽然第一场snailMailOnly显示器的选择YesNo正确,第二个字段snailMailInvoice只显示YN

我在这里做错了什么?

感谢

编辑 - 增加接触模型代码:

class System_Contact(models.Model): 
IS_MAIN_CONTACT_CHOICES = (
    ('Y', 'Yes'), 
    ('N', 'No'), 
) 

IS_SYSTEM_OWNER_CHOICES = (
    ('Y', 'Yes'), 
    ('N', 'No'), 
) 

IS_RESSY_CONTACT_CHOICES = (
    ('Y', 'Yes'), 
    ('N', 'No, this is a commercial contact'), 
) 

TRADE_CHOICES = (
    ('EL', 'Electrician'), 
    ('LA', 'Landscaper'), 
    ('PL', 'Plumber'), 
    ('TR', 'Trencher'), 
) 

SNAIL_MAIL_CHOICES = (
    ('Y', 'Yes'), 
    ('N', 'No'), 
) 

SNAIL_MAIL_INVOICE_CHOICES = (
    ('Y', 'Yes'), 
    ('N', 'No'), 
) 


firstInitial = models.CharField(max_length = 10, verbose_name = 'First Initial', blank = True, null = True) 
firstName = models.CharField(max_length = 60, verbose_name = 'First Name', blank = True, null = True) 
lastName = models.CharField(max_length = 160, verbose_name = 'Last Name', blank = True, null = True) 
phonetically = models.CharField(max_length = 100, verbose_name = 'Phonetically', blank = True, null = True) 
companyName = models.CharField (max_length = 160, verbose_name = 'Company Name', blank = True, null = True) #Only used for Commercial Owners, no other field needed 
homePhone = models.CharField(max_length = 60, verbose_name = 'Home Phone Number', blank = True, null = True) 
officePhone = models.CharField(max_length = 60, verbose_name = 'Office Phone Number', blank = True, null = True) 
cellPhone = models.CharField(max_length = 60, verbose_name = 'Cell Phone Number', blank = True, null = True) 
faxNumber = models.CharField (max_length= 60, blank=True, null=True, verbose_name = 'Fax Number') 
isMainContact = models.CharField (max_length = 3, verbose_name = 'Is the Main Contact?', choices = IS_MAIN_CONTACT_CHOICES, default='N') 
isRessyContact = models.CharField (max_length = 3, verbose_name = 'Is this a Ressy Contact?', choices = IS_RESSY_CONTACT_CHOICES, default='Y') 

isArchived = models.BooleanField(verbose_name = 'Archived?', default = False) 
systemOwner = models.CharField (max_length = 3, verbose_name = 'Is a System Owner?', choices = IS_SYSTEM_OWNER_CHOICES, default='N') #this is just a flag to say they own a system 
worksFor = models.CharField (max_length = 70, verbose_name = 'Works For', blank = True, null = True) 
tradeType = models.ForeignKey(Contact_Trade, blank=True, null=True, verbose_name='Trade') 
emailAddress = models.EmailField(verbose_name = 'Email Address', blank = True, null = True) 

billingAddress = models.CharField(max_length = 150, verbose_name = 'Billing Address', blank=True, null=True) 
billingCity = models.CharField(max_length = 90, verbose_name = 'Billing City', blank=True, null=True) 
billingProvince = models.CharField(max_length = 30, verbose_name = 'Billing Province', blank=True, null=True) 
billingPostalCode = models.CharField(max_length = 10, verbose_name = 'Billing Postal Code', blank=True, null=True) 
snailMailOnly = models.CharField(max_length = 3, verbose_name = 'Snail Mail Only?', choices = SNAIL_MAIL_CHOICES, default='Y') 
snailMailInvoice = models.CharField(max_length = 3, verbose_name = 'Snail Mail Invoice?', choices = SNAIL_MAIL_INVOICE_CHOICES, default='Y') 
+0

发布contact.get_snailMailInvoice_display的代码。问题可能在那里。 – DTing

+0

@Ding - 添加了整个“联系人”模型。不知道你在找什么。 'get_FOO_display'是一个标准的Django标签。 – Garfonzo

+0

对不起,在我的django上有点生疏。肯定地尝试克里斯普拉特的建议,但你有没有尝试用你的模板中的SNAIL_MAIL_CHOICES替换SNAIL_MAIL_INVOICE_CHOICES并查看重启后会发生什么?在附注中,DEFAULT_Y_N_CHOICES而不是所有那些“不同”的选择可能需要考虑? – DTing

回答

0

好的 - 我想通了。

我意识到有一件事与我的CSV文件中的snailMailInvoice字段是每行上的LAST字段有关。因此,在行的末尾有一个回车。我认为这是一个\n - 因此在我的MySQL命令导入CSV,我国terminated by '\n'

但是,MySQL在每行上都选取了'\ r',并将其添加到snailMailInvoice字段中。因此,每个记录都有一个Y或一个N\r附加。

我修改了我的MySQL导入语句:lines terminated with '\r\n'现在一切都按预期工作。

经验教训。

感谢您的帮助。

0

没有理由为根据您发布的代码。 “Y”和“N”在一个点上的显示值为SNAIL_MAIL_CHOICES?您的Web服务器可能不会拉取最近的代码。

如果您正在开发中,请尝试杀死runserver(CTRL + C)并重新启动它。

如果您正在生产,请重新启动您的网络服务器它所代理的进程(如果有的话),如uwsgi。

您还可以尝试删除任何在您的项目中放置的*.pyc文件。 * nix外壳中的快速简单的方法是(从您的项目目录):

$ find . -name="*.pyc" -exec rm {}\; 
+0

不,选项一直是“是”和“否”。我重新启动了测试服务器(尚未投入生产),没有任何变化。我必须说我很困惑! – Garfonzo

+0

请尝试Chris Pratt的建议。重新启动服务器不会删除预编译的.pyc文件。 –

+0

我应该提到我也删除了.pyc文件并且没有变化 - 我仍然得到'Y'或'N' - 不是或不是。 – Garfonzo

相关问题