Is there a way of creating a generic view to be used by several different models? I have many models in my project and do not want to have to create a view and serializer for each of them.
Assume this is a simple solution but I have spent hours googling with no results.
Within the Django REST framework API guide Generic views Examples the following code snippet is shown:
url(r'^/users/', ListCreateAPIView.as_view(model=User), name='user-list')
Which suggests this is possible but the example does not seem to be complete (or my understanding is not complete).
This is my attempt:
url.py
url(r'^foomodel/', views.GenericViewSet.as_view(model = Foomodel) ),
views.py
class GenericViewSet(generics.ListAPIView):
model = User # this is over ridden by the url
queryset = model.objects.all()
serializer_class = BaseSerializer
ordering_fields = '__all__'
serializers.py
class BaseSerializer(serializers.ModelSerializer):
class Meta:
model = None
And of cause this fails as the serializer does not like model = None or any variation that I can think of.
Any suggestions as to how this should be addressed?