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)
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/1987xa0» n'est pas valide. Le format correct est AAAA-MM-JJ."]
[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:
- use different format for forms and models?
- override somehow this parameter?
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?
Any insight is welcome
django datetime django-models django-forms date-format
add a comment |
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)
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/1987xa0» n'est pas valide. Le format correct est AAAA-MM-JJ."]
[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:
- use different format for forms and models?
- override somehow this parameter?
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?
Any insight is welcome
django datetime django-models django-forms date-format
add a comment |
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)
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/1987xa0» n'est pas valide. Le format correct est AAAA-MM-JJ."]
[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:
- use different format for forms and models?
- override somehow this parameter?
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?
Any insight is welcome
django datetime django-models django-forms date-format
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)
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/1987xa0» n'est pas valide. Le format correct est AAAA-MM-JJ."]
[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:
- use different format for forms and models?
- override somehow this parameter?
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?
Any insight is welcome
django datetime django-models django-forms date-format
django datetime django-models django-forms date-format
asked Nov 22 at 20:58
David Zarebski
179111
179111
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The caracter xa0 is related to iso8859-1 encodage. Here is an example:
>>> a = b'xa027/10/1987xa0'
>>> a.decode('iso8859-1').strip()
'27/10/1987'
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')
Otherwise if the type of birth_date is a string, you can easly do:
birth_date = form["birthday"].value().replace(u'xa0', u'')
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)
Bonus: if you need an aware datetime object, think of using pytz module.
form["birthday"].value() is a str but the replace(u'xa0', u'') method does not seem to work: I still get the same error: «xa027/10/1987xa0» n'est pas valide
– David Zarebski
Nov 23 at 9:42
1
issue solved. Thanks @Chiheb. I naively thought that form["birthday"].value() was typed as a date rather than a string. Makes more sense now
– David Zarebski
Nov 23 at 9:55
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437913%2fdjango-datefield-format-impossible-to-validate-and-save-to-model%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The caracter xa0 is related to iso8859-1 encodage. Here is an example:
>>> a = b'xa027/10/1987xa0'
>>> a.decode('iso8859-1').strip()
'27/10/1987'
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')
Otherwise if the type of birth_date is a string, you can easly do:
birth_date = form["birthday"].value().replace(u'xa0', u'')
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)
Bonus: if you need an aware datetime object, think of using pytz module.
form["birthday"].value() is a str but the replace(u'xa0', u'') method does not seem to work: I still get the same error: «xa027/10/1987xa0» n'est pas valide
– David Zarebski
Nov 23 at 9:42
1
issue solved. Thanks @Chiheb. I naively thought that form["birthday"].value() was typed as a date rather than a string. Makes more sense now
– David Zarebski
Nov 23 at 9:55
add a comment |
The caracter xa0 is related to iso8859-1 encodage. Here is an example:
>>> a = b'xa027/10/1987xa0'
>>> a.decode('iso8859-1').strip()
'27/10/1987'
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')
Otherwise if the type of birth_date is a string, you can easly do:
birth_date = form["birthday"].value().replace(u'xa0', u'')
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)
Bonus: if you need an aware datetime object, think of using pytz module.
form["birthday"].value() is a str but the replace(u'xa0', u'') method does not seem to work: I still get the same error: «xa027/10/1987xa0» n'est pas valide
– David Zarebski
Nov 23 at 9:42
1
issue solved. Thanks @Chiheb. I naively thought that form["birthday"].value() was typed as a date rather than a string. Makes more sense now
– David Zarebski
Nov 23 at 9:55
add a comment |
The caracter xa0 is related to iso8859-1 encodage. Here is an example:
>>> a = b'xa027/10/1987xa0'
>>> a.decode('iso8859-1').strip()
'27/10/1987'
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')
Otherwise if the type of birth_date is a string, you can easly do:
birth_date = form["birthday"].value().replace(u'xa0', u'')
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)
Bonus: if you need an aware datetime object, think of using pytz module.
The caracter xa0 is related to iso8859-1 encodage. Here is an example:
>>> a = b'xa027/10/1987xa0'
>>> a.decode('iso8859-1').strip()
'27/10/1987'
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')
Otherwise if the type of birth_date is a string, you can easly do:
birth_date = form["birthday"].value().replace(u'xa0', u'')
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)
Bonus: if you need an aware datetime object, think of using pytz module.
edited Nov 22 at 21:26
answered Nov 22 at 21:15
Chiheb Nexus
4,79531527
4,79531527
form["birthday"].value() is a str but the replace(u'xa0', u'') method does not seem to work: I still get the same error: «xa027/10/1987xa0» n'est pas valide
– David Zarebski
Nov 23 at 9:42
1
issue solved. Thanks @Chiheb. I naively thought that form["birthday"].value() was typed as a date rather than a string. Makes more sense now
– David Zarebski
Nov 23 at 9:55
add a comment |
form["birthday"].value() is a str but the replace(u'xa0', u'') method does not seem to work: I still get the same error: «xa027/10/1987xa0» n'est pas valide
– David Zarebski
Nov 23 at 9:42
1
issue solved. Thanks @Chiheb. I naively thought that form["birthday"].value() was typed as a date rather than a string. Makes more sense now
– David Zarebski
Nov 23 at 9:55
form["birthday"].value() is a str but the replace(u'xa0', u'') method does not seem to work: I still get the same error: «xa027/10/1987xa0» n'est pas valide
– David Zarebski
Nov 23 at 9:42
form["birthday"].value() is a str but the replace(u'xa0', u'') method does not seem to work: I still get the same error: «xa027/10/1987xa0» n'est pas valide
– David Zarebski
Nov 23 at 9:42
1
1
issue solved. Thanks @Chiheb. I naively thought that form["birthday"].value() was typed as a date rather than a string. Makes more sense now
– David Zarebski
Nov 23 at 9:55
issue solved. Thanks @Chiheb. I naively thought that form["birthday"].value() was typed as a date rather than a string. Makes more sense now
– David Zarebski
Nov 23 at 9:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437913%2fdjango-datefield-format-impossible-to-validate-and-save-to-model%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown