Commit 63dec27c authored by Mark Steadman's avatar Mark Steadman

Supports parity between GET and PUT/POST/PATCH in datetime fields

parent e4701454
from api.resources import registry
from api.utils import get_id_field
from django.forms import ModelChoiceField, ModelMultipleChoiceField
from django.forms import (
DateField, DateTimeField,
ModelChoiceField, ModelMultipleChoiceField,
from django.utils.dateparse import parse_datetime
from .packing import PackingMixin
from .resources import ResourceMixin
......@@ -40,3 +46,24 @@ class ModelMixin(ResourceMixin, PackingMixin):
for name, field in form.fields.items():
if isinstance(field, (ModelChoiceField, ModelMultipleChoiceField)):
field.to_field_name = get_id_field(field.queryset.model)
if isinstance(field, DateField):
field.input_formats = ['%Y-%m-%d']
if isinstance(field, DateTimeField):
def mp_datetimefield_to_python(value):
if value is None:
value = value.strip()
return parse_datetime(value)
except (ValueError, TypeError):
raise ValidationError(
field.input_formats = ['%Y-%m-%dT%H:%M:%S%z']
field.to_python = mp_datetimefield_to_python
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment