Make CreateModelMixin.create return the serialization of created instance #8469
Unanswered
Liorinco
asked this question in
Ideas & Suggestions
Replies: 1 comment
-
|
Hi again! Here my update suggestion: class CreateModelMixin:
"""
Create a model instance.
"""
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
instance = self.perform_create(serializer)
serializer = self.get_serializer(instance=instance)
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
def perform_create(self, serializer):
return serializer.save()
def get_success_headers(self, data):
try:
return {'Location': str(data[api_settings.URL_FIELD_NAME])}
except (TypeError, KeyError):
return {} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi!
I would like to add
read_onlyfields usingcreate(POST) operation fromGenericViewSetmixed withCreateModelMixin.But this option is not allowed, due to
createserializes client inputs, then saves the data using the data model and returns previous serialized data, instead of newly created instance.Why this behavior works like this? Isn't it a better solution to let people use
read_onlyfeature also forcreateaction?Regards,
Beta Was this translation helpful? Give feedback.
All reactions