2

Per specification I have date and time input in UnixTimeStamp Also, I need check that I have only digits on input (nor something like this "2011-12-12") I have forms.DataTimeField and model.DataTimeField now.

I decided to modify this in such way: Added UnixTimeStampField(forms.RegexField) - here I check with regular expression, that I have only digits.

But I got problem with model, since UnixTimeStampField(forms.RegexField) is based on string and need to be converted to datatime. I don't like this solution.

How I can create something like UnixTimeStampField(forms.DateTimeField)? I tried to do this, but then I have "123321" on input I got "Incorrect parameter" from DateTimeField validators.

Stedy
  • 7,359
  • 14
  • 57
  • 77
Gamgy
  • 23
  • 4

1 Answers1

3

You don't need a custom model and/or form field for this. Just use the built-in forms.DateTimeField and models.DateTimeField, then add the following settings to your settings.py:

DATETIME_FORMAT = "U"
DATETIME_INPUT_FORMATS = ("%s",)

Read more about this settings here.

Simon Kagwi
  • 1,636
  • 2
  • 19
  • 25
  • 1
    How can I do this for just one of my forms and not as a global setting? – nknj Sep 28 '12 at 06:04
  • You could override the field you are using for the date and specify which format the field will accept. https://github.com/django/django/blob/master/django/forms/fields.py#L395 – Ernest Jumbe Dec 09 '12 at 01:02