So you don't have any control over the API call? Ok then, you can update
your serializer to take a string representing a symbol and grab the correct
address object, or throw an error if it doesn't exist:

http://www.django-rest-framework.org/api-guide/serializers/#validation

class UserPrefSerializer(serializers.ModelSerializer):

    def validate_address(self, value):
        try:
            address_obj = Address.objects.get(symbol=value)
        except Address.DoesNotExist:
            raise serializers.ValidationError("Address does not exist.")

        return address_obj

    def validate_prefs(self, value):
        # this loop searches your choices list and converts "Dislikes" to
1, etc.
        for pref_item in UserPrefs.Preferences:
            if pref_item[1] == value:
                return pref_item[0]

        raise serializers.ValidationError("Invalid pref value.")

    class Meta:
        model = UserPrefs
        fields = ('id', 'address', 'prefs')


Now if you check serializer.validated_data, you should see the correct
address object. As a bonus, there is also a validator for the string
versions of prefs as well.

I would highly recommend you go with the solution I posted, using the
modified UserPrefSerializer above. The original curl commands you posted up
should work with this modified serializer, and you wouldn't need to supply
the user (it gets populated automatically):

curl -H  "Authorization: Bearer $usertoken" -H "Content-Type:
application/json" -X POST -d '{"address":"XYZ","prefs":"Likes"}' http://
${endpoint}/addPrefs

Putting in an invalid address or prefs value should result in a validation
error.

(Full disclosure, this is untested)

-James


On Wed, Jun 3, 2015 at 8:43 AM, Shekar Tippur <ctip...@gmail.com> wrote:

> James,
>>
>
> My apologies. stock_id came in as a artefact of a bad copy/paste.
> The only deviation from what you have said is that the api call comes with
> a address string and not a id. But your solution works perfectly well as
> well.
> Here are the right ones:
>
> serializer=UserPrefSerializer(data=request.data)
>
> seriazer object will be populated with post request data.
> address_obj=Address.objects.get(symbol=request.data['address'])
> user_obj=User.objects.get(username=request.user)
>
> serializer.validated_data['address_id']=address_obj.id
> serializer.validated_data['user_id']=user_obj.id
>
> Thanks again,
> S
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/6782c6e4-451e-4285-9c2b-c9e9abdd3090%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/6782c6e4-451e-4285-9c2b-c9e9abdd3090%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2Be%2BciWFSXKRfKQ2SUtNoQRjM3EOJKwOsFzrjkYGrjYxnQREzA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to