1

In continuation to this question, can you please give an example of a valid input json to simultaneously create a new User and UserProfile?

While this successfully creates a new user on User end-point:

{
    "username": "newuser", 
    "password": "abc"
}

But this fails on UserProfile end-point:

{
    "user":{
            "username": "newuser2", 
            "password": "abc"
    }
    "biography": "1",
}

Returning:

{
    "user": [
        "This field cannot be null."
    ]
}
1
  • Usually these take a resource uri pointing to another resource for related objects but I can't say for sure that DjangoRestFramework does since I haven't used it. Commented May 13, 2013 at 12:43

1 Answer 1

0

What you're looking for is read+write support for nested resources which will soon be supported, but is not as of yet.

However, if you link your user profile model to the user model with a HyperlinkedRelatedField you could first create a user and then create a user profile with the created user's URI as user argument. Assuming you have the following user profile model and serializer::

# Model:
class UserProfile(models.Model):
country = models.CharField(max_length=127)
wants_newsletter = models.BooleanField(default=False)

# Serializer:
class UserProfile(serializers.ModelSerializer):
user = serializers.HyperlinkedRelatedField('user-detail')

class Meta:
    fields = ('country', 'wants_newsletter')

... and a user object at users/1/, you could create a new user profile for the given user with the following POST request to the user profile endpoint::

{
    "country": "Switzerland",
    "user": "/users/1/"
}

If that's too many requests for you, take a a look at the docs.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.