What is the control flow of django rest system

I am developing api for webapp. At first I used tastypie and switched to django-rest-framework (drf) . Drift seems very easy to me. What I intend to do is create a nested user profile object. My models below

 from django.db import models from django.contrib.auth.models import User class nestedmodel(models.Model): info = models.CharField(null=True, blank=True, max_length=100) class UserProfile(models.Model): add_info = models.CharField(null=True, blank=True, max_length=100) user = models.OneToOneField(User) nst = models.ForeignKey(nestedmodel) 

I have other models that are related to the "alien". My Serials below

 from django.contrib.auth.models import User, Group from rest_framework import serializers from quickstart.models import UserProfile, nestedmodel class NestedSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = nestedmodel fields = ('info', ) class UserSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = User fields = ('url', 'username', 'email', 'groups') class GroupSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Group fields = ('url', 'name') class UserProfileSerializer(serializers.HyperlinkedModelSerializer): user = UserSerializer() nst = NestedSerializer() class Meta: model = UserProfile user = UserSerializer(many=True) nested = NestedSerializer(many=True) fields = ('nst', 'user') 

I can override methods like create(self, validated_data): without any problems. But what I want to know is to which method should the response returned by create() goes , or, in other words, Which method calls create() . In tastypie Resources.py this is an override file for implementing custom methods. And Resource.py contains the order in which the method is called. What is a drf file that does the same thing and illustrates a control flow like Resources.py in tastypie ?.

+5
source share
2 answers

So the stream looks something like this:

  • Viewset create() method, which is implemented in CreateModelMixin
  • This creates a serializer and validates it. Once it is valid, it uses viewet perform_create()
  • This calls the serializer save() method
  • This, in turn, calls the create() or update() serializer, depending on whether the instance was passed to the serializer (which was not in step 1)
  • create() or update() then create an instance / update, which is then saved to serializer.instance
  • Viewset then returns a response with data coming from serializer.data
  • serializer.data is actually a serializer property that is responsible for serializing an instance in a dict
  • To serialize data, to_representation() .
  • Then the response data (Python dict) is transferred to the output format via renderers , which can be json, xml, etc.

And Resource.py contains the order in which the method is called. What is a drf file that accomplishes the same goal and illustrates a control flow such as Resources.py in tastypie ?.

Suppose this is a combination of files. It is probably best to think in terms of the classes / concepts you are referring to, since in DRF you can inherit from several things to create your classes. So, everything that glues everything together, viewsets . Then there are various kinds of mixins that actually glue the view to the serializer and various CRUD operations.

+9
source

I myself figured out the second part of the question. You can get / create an object using custom code in overriden def create(self, request, *args, **kwargs): in views.py . The code is below. Once again, for clarity, this is views.py not serializers.py. Also access to json with published values ​​can be obtained 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 :)

+1
source

All Articles