我有一个使用MySQL作为后端的Django应用程序。我在原始MySQL记录显示一个值时遇到困难,但Django在Web应用程序中提供了其他内容。
例如,我有一个客户数据表。每条记录中的一个字段称为snailMailInvoice
,是Y / N选项 - 默认为Y(varchar
类型)。
+-------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+----------------+
| snailMailInvoice | varchar(3) | NO | | Y | |
原始MySQL:
select * from systems_system_contact where lastName="SomeClient";
...a bunch of other fields... | snailMailInvoice |
...a bunch of other fields... | N
然后,在Django App中,表单显示Y
(另一个选项)。这就像Django App无法看到MySQL值,所以它默认为Y
。如果,通过Django App,我选择N
并保存表单,那么值在Django中保持N
。
为什么会这样?
编辑 - 添加一些代码
Forms.py:
class System_Contact_Form(ModelForm):
class Meta:
model = System_Contact
exclude = ('isMainContact', 'systemOwner', 'companyName', 'isRessyContact')
Views.py:
def contact_details(request, scID):
redirect_to = request.REQUEST.get('next', '/systems/contacts/')
if request.method == 'POST':
syscontEdit = System_Contact.objects.get(pk=scID)
form = System_Contact_Form(request.POST, instance=syscontEdit)
if form.is_valid():
form.save()
return HttpResponseRedirect(redirect_to)
else:
syscontView = System_Contact.objects.get(pk=scID)
form = System_Contact_Form(instance=syscontView)
c = {
'form':form,
'cancel':redirect_to
}
return render_to_response('pages/systems/contact_details.html', c, context_instance=RequestContext(request))
Models.py:
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 :(得分:0)
好的 - 我明白了。
我意识到有一些事实与我的CSV文件中的snailMailInvoice
字段是每行的最后一个字段有关。因此,在该行的末尾有一个回车。我假设这是一个\n
- 因此在我的MySQL命令中导入CSV,我说明terminated by '\n'
。
但是,MySQL在每条线路上都选择了'\ r'并将其添加到snailMailInvoice
字段。因此,每条记录都有一个Y
或N
附加\r
。
我修改了我的MySQL import语句:lines terminated with '\r\n'
现在一切都按预期工作了。
经验教训。
感谢您的帮助。