I am developing an api for a webapp. I was initially using tastypie and switched to django-rest-framework (drf)
. Drf seems very easy to me. What I intend to do is to create nested user profile object. My models are as 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 have Foreignkey Relation. My Serializers are as 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 issues. 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
is the file to override to implement custom methods. 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?.
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):
inviews.py
. Code is as pasted below. Once Again, for clarity this isviews.py
not serializers.py. Also json with posted values can be accessed fromrequest.DATA
Thanks for the help @miki275 :)
So the flow goes something like:
create()
method which is implemented inCreateModelMixin
perform_create()
save()
methodcreate()
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
serializer.data
serializer.data
is actually a property on serializer which is responsible for serializing the instance to a dictto_representation()
is used.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.