Hello,
I was trying to find a way to change the serializer field conditionally
based on the request. When the request is GET, then I would like to have a
nested serializer, otherwise I'm not interested in the details of the
object and rather use the list of pk s to process the data. Is this
approach advisable? Is there a best practice or do I have to change my
mindset altogether?
Below is my approach so far:
class FooSerializer(ModelSerializer):
bar = SerializerMethodField('get_bar')
def get_bar(self, obj):
req = self.context['request']
if(req.method == 'GET'):
bar = BarSerializer(many=True) # shouldn't I pass the data
related to 'Bar' here?
else:
bar = PrimaryKeyRelatedField(many=True, queryset=Bar.objects.
filter(foo=obj)) # is this correct?
# insert class Meta here...
Thanks in advance,
Ege
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.