Trouble with DateField
Hi everyone, I need a sanity check here. I'm using a jQueryUI DatePicker, with the dateFormat option set to 'yy-mm-dd', which is returning a date in - MM-DD format, if I check my request.POST values. I have a DateField - "effective_from", on my model, and am using a corresponding ModelForm. When I check the form.cleaned_data ['effective_from'], I get a Python datetime object as: 2009-04-13. However, I'm getting an exception error for the view, saying: ValidationError at /codes/create/ Enter a valid date in -MM-DD format. I have also parsed the date by hand: , MM, DD = map(int, request.POST['effective_from'].split('-')) effective_from = datetime.date(, MM, DD) The portion of the view in question is: form = ActivityCodeForm(request.POST) if form.is_valid(): activity_code = form.save(commit=False) activity_code.created_by = request.user activity_code.category = Category.objects.get(pk=int (form.cleaned_data['kategory'])) activity_code.effective_from = form.cleaned_data ['effective_from'] activity_code.slug = slugify(form.cleaned_data['title']) activity_code.save() Can anyone see what I'm doing wrong here? TIA, Brandon --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Trouble with DateField
On Apr 13, 11:47 am, Brandon Taylor wrote: > Hi everyone, > > I need a sanity check here. I'm using a jQueryUI DatePicker, with the > dateFormat option set to 'yy-mm-dd', which is returning a date in - > MM-DD format, if I check my request.POST values. > > I have a DateField - "effective_from", on my model, and am using a > corresponding ModelForm. When I check the form.cleaned_data > ['effective_from'], I get a Python datetime object as: 2009-04-13. > > However, I'm getting an exception error for the view, saying: > ValidationError at /codes/create/ > Enter a valid date in -MM-DD format. > > I have also parsed the date by hand: > , MM, DD = map(int, request.POST['effective_from'].split('-')) > effective_from = datetime.date(, MM, DD) > > The portion of the view in question is: > form = ActivityCodeForm(request.POST) > if form.is_valid(): > activity_code = form.save(commit=False) > activity_code.created_by = request.user > activity_code.category = Category.objects.get(pk=int > (form.cleaned_data['kategory'])) > activity_code.effective_from = form.cleaned_data > ['effective_from'] > activity_code.slug = slugify(form.cleaned_data['title']) > activity_code.save() > > Can anyone see what I'm doing wrong here? Here: activity_code.effective_from = form.cleaned_data['effective_from'] You are assigning a string to what I assume is a models.DateTimeField or DateField. I don't think that works. You need to turn the string into a Python datetime object first. I think this is done automatically for you if you were using a ModelForm. Best, BN --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Trouble with DateField
Hi Brian, Yes, I've also made certain that the value returned for effective date from form.cleaned_data['effective_date'] is a valid Python datetime object. Something seems to be amiss. The exception is happening in: django/db/ models/fields/__init__.py in to_python, line 473 If I put a print statement in there to print the 'value' variable handed to the 'to_python' function, it doesn't return None or the value, in fact it prints an empty line, which is making me think it's an empty string...? b On Apr 13, 12:06 pm, Brian Neal wrote: > On Apr 13, 11:47 am, Brandon Taylor wrote: > > > > > Hi everyone, > > > I need a sanity check here. I'm using a jQueryUI DatePicker, with the > > dateFormat option set to 'yy-mm-dd', which is returning a date in - > > MM-DD format, if I check my request.POST values. > > > I have a DateField - "effective_from", on my model, and am using a > > corresponding ModelForm. When I check the form.cleaned_data > > ['effective_from'], I get a Python datetime object as: 2009-04-13. > > > However, I'm getting an exception error for the view, saying: > > ValidationError at /codes/create/ > > Enter a valid date in -MM-DD format. > > > I have also parsed the date by hand: > > , MM, DD = map(int, request.POST['effective_from'].split('-')) > > effective_from = datetime.date(, MM, DD) > > > The portion of the view in question is: > > form = ActivityCodeForm(request.POST) > > if form.is_valid(): > > activity_code = form.save(commit=False) > > activity_code.created_by = request.user > > activity_code.category = Category.objects.get(pk=int > > (form.cleaned_data['kategory'])) > > activity_code.effective_from = form.cleaned_data > > ['effective_from'] > > activity_code.slug = slugify(form.cleaned_data['title']) > > activity_code.save() > > > Can anyone see what I'm doing wrong here? > > Here: > > activity_code.effective_from = form.cleaned_data['effective_from'] > > You are assigning a string to what I assume is a models.DateTimeField > or DateField. I don't think that works. You need to turn the string > into a Python datetime object first. I think this is done > automatically for you if you were using a ModelForm. > > Best, > BN --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Trouble with DateField
Well, I'm not sure what the matter was, maybe some bad cache or something, but the problem seems to have corrected itself. Weird! On Apr 13, 12:06 pm, Brian Neal wrote: > On Apr 13, 11:47 am, Brandon Taylor wrote: > > > > > Hi everyone, > > > I need a sanity check here. I'm using a jQueryUI DatePicker, with the > > dateFormat option set to 'yy-mm-dd', which is returning a date in - > > MM-DD format, if I check my request.POST values. > > > I have a DateField - "effective_from", on my model, and am using a > > corresponding ModelForm. When I check the form.cleaned_data > > ['effective_from'], I get a Python datetime object as: 2009-04-13. > > > However, I'm getting an exception error for the view, saying: > > ValidationError at /codes/create/ > > Enter a valid date in -MM-DD format. > > > I have also parsed the date by hand: > > , MM, DD = map(int, request.POST['effective_from'].split('-')) > > effective_from = datetime.date(, MM, DD) > > > The portion of the view in question is: > > form = ActivityCodeForm(request.POST) > > if form.is_valid(): > > activity_code = form.save(commit=False) > > activity_code.created_by = request.user > > activity_code.category = Category.objects.get(pk=int > > (form.cleaned_data['kategory'])) > > activity_code.effective_from = form.cleaned_data > > ['effective_from'] > > activity_code.slug = slugify(form.cleaned_data['title']) > > activity_code.save() > > > Can anyone see what I'm doing wrong here? > > Here: > > activity_code.effective_from = form.cleaned_data['effective_from'] > > You are assigning a string to what I assume is a models.DateTimeField > or DateField. I don't think that works. You need to turn the string > into a Python datetime object first. I think this is done > automatically for you if you were using a ModelForm. > > Best, > BN --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---