Django DateField格式无法验证并保存到模型 [英] Django DateField format impossible to validate and save to model
问题描述
老问题,我知道.但是我仍然找不到可行的解决方案,因此我可能错过了一些显而易见的事情.这是我的表格
Old issue, I know. But I still could not find a working solution so I may have missed something obvious. Here is my form
class AddPatientForm(forms.Form):
last_name = forms.CharField(label='Nom', max_length=40)
first_name = forms.CharField(label='Prénom', max_length=40)
birthday = forms.DateField(label='date de naissance',
widget=forms.DateInput(format='%d/%m/%Y',attrs={'placeholder': '31/03/1989'}),
input_formats=['%d/%m/%Y',])
它遵循这种格式约定,必须这样做.这是我的模型:
It follows this format convention and has to do so. Here is my model:
class Patients(models.Model):
first_name = models.CharField(max_length=40)
last_name = models.CharField(max_length=40)
birth_date = models.DateField()
inscription = models.DateTimeField(auto_now_add=True)
这是我在 setting.py 中尝试解决的问题
And here is what I tried in setting.py to get rid of the issue
DATE_FORMAT = "d-m-Y"
DATE_INPUT_FORMATS = ['%d-%m-%Y']
USE_L10N = False
尽管如此,我仍然遇到同样的问题:
Despite of this, I still got the same issue:
form = AddPatientForm(request.POST)
if form.is_valid():
form.clean()
d = Patients(first_name= form["first_name"].value(),
last_name= form["last_name"].value(),
birth_date= form["birthday"].value())
d.save()
>>>>["Le format de date de la valeur «\xa027/10/1987\xa0» n'est pas valide. Le format correct est AAAA-MM-JJ."]
[免责声明]我不是要覆盖模型格式约定,我知道日期"是否真正存储在数据库"中的问题是无关紧要的(不同从一个DBMS到另一个DBMS; django模型对此是不可知的).但这就是***与如此简单的任务作斗争的关键.我为什么不能:
[disclaimer] I am not looking to override the model format convention and I know that the question of how dates "are really stored in db" is irrelevant (different from one DBMS to an other; django models are agnostic about it). But that's such a pin in the *** to struggle with such a simple task. Why can't I:
- 对表单和模型使用不同的格式吗?
- 以某种方式覆盖此参数?
不是一个具有显式格式的DateField的事实,该显式格式带有显式参数,足以使验证者理解什么是什么?我想念什么?
Isn't the very fact of being a DateField with an explicit format provided with explicit parameters sufficient for the validator to understand what's what? What am I missing?
欢迎任何见识
推荐答案
角色 \ xa0
与 iso8859-1
编码有关.这是一个示例:
The caracter \xa0
is related to iso8859-1
encodage. Here is an example:
>>> a = b'\xa027/10/1987\xa0'
>>> a.decode('iso8859-1').strip()
'27/10/1987'
因此,在您的问题中,如果 birth_date = form ["birthday"].value()
是一个 bytes
对象,则需要首先以常规方式对其进行解码具有有效日期格式的字符串以进行进一步操作:
So, in your question, if birth_date= form["birthday"].value()
is a bytes
object, you need first to decode it in a regular string with valid date format for further manipulaiton:
birth_date = form["birthday"].value().decode('iso8859-1')
否则,如果 birth_date
的类型是字符串,则可以轻松做到:
Otherwise if the type of birth_date
is a string, you can easly do:
birth_date = form["birthday"].value().replace(u'\xa0', u'')
然后,为了将日期存储在数据库中,您需要一个有效的 datetime
对象.您可以将字符串转换为有效的Python日期时间对象,例如以下示例:
Then, in order to store the date in your Database you need a valid datetime
object. You can convert your string to a valid Python datetime object like this example:
>>> from datetime import datetime
>>> datetime.strptime('27/10/1987', '%d/%m/%Y')
datetime.datetime(1987, 10, 27, 0, 0)
奖金:如果您需要一个已知的日期时间对象,请考虑使用 pytz
模块.
Bonus: if you need an aware datetime object, think of using pytz
module.
这篇关于Django DateField格式无法验证并保存到模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!