0

I'm providing JSON to a view like so -

{
    "username": "name",
    "first_name": "john",
    "last_name": "doe",
    "email": "[email protected]",
    "profile": {
        "company": "abc corp"
    }
}

I'm then passing it into the following view to post -

    def post(self, request, format=None):
        uuid = generate_uuid(request.data.get('username'))
        data = {'username': request.data.get('username'),
                'first_name': request.data.get('first_name'),
                'last_name': request.data.get('last_name'),
                'email': request.data.get('email'),
                'company': request.data.get('profile').('company'),
                'uuid': str(uuid)}
        serializer = UserSerializer(data=data)
        if serializer.is_valid():
            print serializer.data
#            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

I'm having difficulty in understanding how to structure the data to maintain the nested value as you can see I'm also including a field for a uuid which will also be in the nested profile object.

These are then being passed into my serializer here -

class UserSerializer(serializers.ModelSerializer):
    profile = UserProfileSerializer()
    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name', 'email', 'profile')

My view currently as it is gives a syntax error on this line -

'company': request.data.get('profile').('company'),

I know it's wrong just not sure how it should be structured.

1 Answer 1

0

Here is the correct version of this line:

'company': request.data.get('profile', dict()).get('company'),

What this does is supply an empty dict as a default argument if the data is missing a 'profile' object, or if it has one, call get('company') on that.

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

4 Comments

hmmm... I don't think I put my question right. I see what this is doing but looks to be flattening the json. I found that just doing 'profile': request.data.get('profile') puts in the nested company automatically but then how to I include the 'uuid' in the nested 'profile'?
@whoisearth What exactly is uuid? Your UserSerializer does not have a uuid field and I'm pretty sure the default Django User model also does not. Where exactly is uuid getting saved?
I'm generating a uuid and storing it in UserProfile model which I'm then linking to User using a OneToOneField.
I marked this as correct but I ultimately ended up using the documentation here - http://stackoverflow.com/questions/19772457/django-rest-framework-creating-custom-user

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.