42👍
So the flow goes something like:
- Viewset’s
create()
method which is implemented inCreateModelMixin
- That creates serializer and validates it. Once valid, it uses viewset’s
perform_create()
- That calls serializer’s
save()
method - That then in turn calls serializer’s either
create()
orupdate()
depending if instance was passed to serializer (which it was not in step 1) create()
orupdate()
then create/update instance which is then saved onserializer.instance
- Viewset then returns response with data coming from
serializer.data
serializer.data
is actually a property on serializer which is responsible for serializing the instance to a dict- To serialize data,
to_representation()
is used. - Then response data (Python dict) is rendered to a output format via renderers which could be json, xml, etc
And Resources.py contains the order in which methods are being called. Which is the file in drf that serves the same purpose and illustrates the control flow like Resources.py in tastypie?.
Guess that would be a combination of files. Its probably better to think in terms of classes/concepts you are touching since in DRF you can inherit from multiple things for create your classes. So the thing which glues everything together are viewsets. Then there are various viewset mixins which actually glue the viewset to the serializer and different CRUD operations.
1👍
I figured out second part of question myself. get/create object can be done by using custom code in overriden def create(self, request, *args, **kwargs):
in views.py
. Code is as pasted below. Once Again, for clarity this is views.py
not serializers.py. Also json with posted values can be accessed from request.DATA
class NestedViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows Nested objects to be viewed or edited.
"""
queryset = nestedmodel.objects.all()
serializer_class = NestedSerializer
def create(self, request, *args, **kwargs):
info = request.DATA['info']
user = User.objects.get(username=request.DATA['user']['username'])
profile = UserProfile.objects.get(user=user)
nst = nestedmodel.objects.create(info=info, user=user, profile=profile)
serialized_obj = serializers.serialize('json', [ nst, ])
json_serialized = json.loads(serialized_obj)
data = json.dumps(json_serialized[0])
return Response(data)
Thanks for the help @miki275 🙂