How do I serialize a many-to-many field into list of something, and return them through rest framework? In my example below, I try to return the post together with a list of
In the serializer on init method you can pass the queryset to the field and rest_framework valide the ids on that queryset
1) first extend your serializer from serializers.ModelSerializer
class YourSerializer(serializers.ModelSerializer):
2) include the field on the meta class
class YourSerializer(serializers.ModelSerializer):
class Meta:
fields = (..., 'your_field',)
3) in the init method:
def __init__(self, *args, **kwargs):
super(YourSerializer, self).__init__(*args, **kwargs)
self.fields['your_field].queryset =
You can limit the queryset for that field under any argument using filter or exclude like normally you do. In case that you want include all just use .objects.all()