3

Possible Duplicate:
Converting string into datetime

In Django I get this error "Enter a valid date/time in YYYY-MM-DD HH:MM[:ss[.uuuuuu]] format." when I try to assign a string "22-DEC-2009" to a DateTimeField in a model.

How is it possible to make DateTimeField accept a date string in format "22-DEC-2009"?

Community
  • 1
  • 1
Vishal
  • 19,879
  • 23
  • 80
  • 93

1 Answers1

9

You can pass the input formats as input_formats argument to DateTimeField, so you can do this

# you can keep a list of formats yourself, or copy from django 1.2 version e.g.
# my_formats = fields.DEFAULT_DATETIME_INPUT_FORMATS + ['%d-%b-%Y']
# for latest django use this
from django.utils.formats import get_format
my_formats = get_format('DATETIME_INPUT_FORMATS')
field = DateTimeField(input_formats=my_formats,...)

If instead you directly want to assign a date-str to models.DateTimeField best way is to just convert it to datetime before hand e.g.

mymodel.date_of_birth = datetime.datetime.strptime("22-DEC-2009", "%d-%b-%Y")
Anurag Uniyal
  • 85,954
  • 40
  • 175
  • 219
  • Isn't accepting argument input_formats for models.DateTimeField. – Vishal Dec 23 '09 at 03:11
  • @Vishal , what does it say? what version are you using? – Anurag Uniyal Dec 23 '09 at 04:29
  • @Anurag Error: "/django/db/models/fields/__init__.py", line 459, in __init__ Field.__init__(self, verbose_name, name, **kwargs) TypeError: __init__() got an unexpected keyword argument 'input_formats'" Django version: "1.1.1" /django/db/models/fields doesn't contain DEFAULT_DATETIME_INPUT_FORMATS but /django/forms/fields.py does (which makes the code run) Useful post: http://stackoverflow.com/questions/466345/converting-string-into-datetime – Vishal Dec 23 '09 at 14:18
  • yes form.fields has it not db – Anurag Uniyal Dec 23 '09 at 15:24
  • input_formats is not supported in django 1.4 – hobs Jan 23 '13 at 02:34
  • 1
    @hobs in that case copy the formats from old version or use new method ( edited answer) – Anurag Uniyal Jan 23 '13 at 16:50
  • @Anurag, thanks for providing a working `formats` import for 1.4, however the argument `input_formats` is not support for django 1.4 DateTimeFields – hobs Jan 24 '13 at 02:11
  • @hobs what do you mean by not supported? `In [2]: from django.forms.fields import DateTimeField In [3]: DateTimeField(input_formats=[]) Out[3]: In [4]: import django In [5]: django.VERSION Out[5]: (1, 4, 0, 'final', 0) ` – Anurag Uniyal Jan 24 '13 at 03:12
  • @Anurag, op was asking about a `models.DateTimeField`, not `forms.DateTimeField`. `In [1]: from django.db.models import DateTimeField` `In [2]: DateTimeField(input_formats=[])` `TypeError: __init__() got an unexpected keyword argument 'input_formats'` – hobs Jan 24 '13 at 03:29
  • @hobs that was already known, see my dec 09 comment, for UI forms.DateTimeField is enough if OP wants to directly assign to model he can easily convert the date-str (see edit) – Anurag Uniyal Jan 24 '13 at 04:39
  • @Anurag, yea, guess I missed that. – hobs Jan 24 '13 at 17:09