CurrentUserDefault on Serializer

42 views
Skip to first unread message

Jenna Pullen

unread,
Aug 16, 2016, 11:43:53 AM8/16/16
to Django REST framework
Hi Guys,

I am having difficulty figuring out how to filter my fields choices by the logged in User. Does anyone know how this works?
 
question = serializers.ChoiceField(choices=Question.objects.filter(user=serializers.CurrentUserDefault())) # tried

Thanks

Kevin Brown

unread,
Aug 16, 2016, 9:37:31 PM8/16/16
to Django REST framework

You probably want to use a relational field (PrimaryKeyRelatedField or similar) and modify the query set used for it within your init method of your serializer. You should have access to the current request through the serializer context, and you can use it for filtering down the values accepted by your field.


--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jenna Pullen

unread,
Aug 17, 2016, 3:46:44 AM8/17/16
to Django REST framework
Thanks Kevin,

That worked!
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

Javier Lázaro

unread,
Oct 5, 2016, 12:02:23 PM10/5/16
to Django REST framework
Hi Jenna.

Can you show me how did yo do it? I'm having the same issue.

Thanks in advance!
Reply all
Reply to author
Forward
0 new messages