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.resources import registry
from api.utils import get_id_field from api.utils import get_id_field
from django.forms import ModelChoiceField, ModelMultipleChoiceField from django.forms import (
DateField, DateTimeField,
ModelChoiceField, ModelMultipleChoiceField,
ValidationError
)
from django.utils.dateparse import parse_datetime
from .packing import PackingMixin from .packing import PackingMixin
from .resources import ResourceMixin from .resources import ResourceMixin
...@@ -40,3 +46,24 @@ class ModelMixin(ResourceMixin, PackingMixin): ...@@ -40,3 +46,24 @@ class ModelMixin(ResourceMixin, PackingMixin):
for name, field in form.fields.items(): for name, field in form.fields.items():
if isinstance(field, (ModelChoiceField, ModelMultipleChoiceField)): if isinstance(field, (ModelChoiceField, ModelMultipleChoiceField)):
field.to_field_name = get_id_field(field.queryset.model) 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:
return
value = value.strip()
try:
return parse_datetime(value)
except (ValueError, TypeError):
raise ValidationError(
field.error_messages['invalid'],
code='invalid'
)
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