1

I am using Python 3.5, Django 2.0 and Django REST Framework 3.7.7 I added django-jalali 3.0.2 to my apps for jalali calelndar compatibility. This app works fine when using admin or shell, but when I send requests using DRF, datetime object is validated using the Gregorian calendar. As a result I cannot use date with a string like 1397-02-31 or 1397-02-30. This is the response I get:

HTTP 400 Bad Request Allow: GET, POST, HEAD, OPTIONS Content-Type: application/json Vary: Accept

{
    "schedule_time": [
        "Enter a valid date/time in YYYY-MM-DD HH:MM[:ss[.uuuuuu]] format."
    ]
}

THis error originates from line 252 of django_jalali/db/models.py

try:  # Seconds are optional, so try converting seconds first.
        t = time.strptime(datetime_obj, '%Y-%m-%d %H:%M:%S')

because srtptime presumes datetime_obj is Datetime object not jDatetime. As mentioned before this is not a problem in admin or shell. I also have tested setting locale to 'fa_IR' and changing the object manager but no luck so far. Is there a clean solution for this or do I have to write my own app?

P.S. I've noticed date_hierarchy in admin only uses Datetime (not jDatetime). I tried changing it but it fails django checks. how can I change this to jalali?

I've also posted this as an issue on django-jalali github page and still waiting for an answer.

4l12
  • 322
  • 2
  • 10
  • `time.strptime` presumes that `datetime_obj` is a string representing a Gregorian date. Since February doesn't have 30 days this would raise an exception. Are you sure django-jalali is compatible with django-rest-framework? It seems to accept only gregorian date strings as input here. – Håken Lid May 07 '18 at 23:35
  • You might have to write a custom [drf serializer field](http://www.django-rest-framework.org/api-guide/fields/#custom-fields). But you should probably clearly distinguish Jalali date strings from ISO 8601 Gregorian dates such as `YYYY-MM-DD`. – Håken Lid May 07 '18 at 23:43

0 Answers0