Django REST framework post array of objects

后端 未结 5 664
挽巷
挽巷 2020-12-16 14:43

I am using Django REST framework for API and Angular SPA with Restangular to communicate with the API. Sometimes, I have to add more than one object using the API and I thin

5条回答
  •  情深已故
    2020-12-16 15:21

    Building on vibhor's answer:

    class ListableViewMixin(object):
        def get_serializer(self, instance=None, data=None, many=False, *args, **kwargs):
            return super(ListableViewMixin, self).get_serializer(
                instance=instance, data=data, many=isinstance(instance, list) or isinstance(data, list),
                *args, **kwargs)
    

    Make your view inherit from this mixin class to automatically determine if a many=True serializer should be used.

提交回复
热议问题