I am creating a Django application, and during development I have created many classes which have a very similar structure, e.g.:
class OneAPIListView(generics.ListAPIView):
queryset = models.One.objects.all()
serializer_class = serializers.OneSerializer
filter_class = OneFilterSet
class TwoAPIListView(generics.ListAPIView):
queryset = models.Two.objects.all()
serializer_class = serializers.TwoSerializer
filter_class = TwoFilterSet
...
class <Name>APIListView(generics.ListAPIView):
queryset = models.<Name>.objects.all()
serializer_class = serializers.<Name>Serializer
filter_class = <Name>FilterSet
I am wondering if there is a way to automatically generate these classes from the list of strings ["One", "Two", ..., <Name>]. I see that there are metaclasses in other SO questions like this, e.g. I could do something like
<Name> = type(<Name> + "APIListView", (generics.ListAPIView,), {'queryset' : ???})
But I am not sure what to put into ??? in my case since the variable is part of an object name.
{'queryset': getattr(models, name).objects.all()}and so on.