Django REST framework: Create/Update object using Related Field

django, django-models, django-rest-framework, rest

Solution

Actually Django Rest Framework has a partial nested support. If you look at the test suite you'll find some. As for bitgeeky question, as said on IRC, this should work. Here's an associated test case: https://github.com/tomchristie/django-rest-framework/blob/2.3.13/rest_framework/tests/test_relations_nested.py#L56

Thinking about this, I think I had this error once. @bitgeeky can you make sure you're sending a json request by adding the content type in the header ? By default posts will be using forms which don't support nested.

Problem

I have two models Auth User model and UserProfile ``` class UserProfile(models.Model): user = models.OneToOneField(User, related_name='profile') name = models.CharField(_lazy(u'Name'), max_length=255) ``` For which I am using these serializers: ``` from rest_framework import serializers from django.contrib.auth.models import User from oneanddone.users.models import UserProfile class UserProfileSerializer(serializers.ModelSerializer): class Meta: model = UserProfile fields = ('name',) class UserSerializer(serializers.ModelSerializer): profile = serializers.RelatedField() class Meta: model = User fields = ('id', 'username', 'email', 'groups', 'profile') ``` But on making a post request ``` requests.post('http://localhost:8000/api/v1/users/', data={"username": "tester", "email": "tester@gmail.com", "profile": [{"name":"testername"}]} ,headers={'Authorization':'Token d81e33c57b2d9471f4d6849bab3cb233b3b30468'}).text ``` I get the following object with a "null" profile field ``` u'{"id": 10, "username": "tester", "email": "tester@gmail.com", "groups": [], "profile": null}' ``` I am not able to figure out how to achieve creation and updation of user profile name along with auth user data. Please let me know how to do this and provide some example for the same.

Original source