[Fixed]-Overriding create method in Django RestFramework to POST data

1๐Ÿ‘

I think the proper way to go about it is not to write your own custom create method, but rather to teach your serializer how to accept the date format you use, e.g.

class SimpleSerializer4(something_here):
    ...
    start_dt = serializers.DateTimeField(format=api_settings.DATETIME_FORMAT, input_formats=['%Y-%m-%d %H:%M:%S'])
    ...

Then later all you have to do is to add CreateModelMixin to you ViewSet and it should work, e.g.

from rest_framework.mixins import CreateModelMixin
...
class DateTimeViewSet(DateTimeMixin, CreateModelMixin, generics.BulkModelViewSet):
...

Docs on DateTimeField here

Docs on extending view sets here

And I found it often very helpful to look how things are done in the rest_framework itself, so here is the link to the source on GitHub

๐Ÿ‘คCrowbarKZ

Leave a comment