I am making a lot of views and serializers for my models that takes all fields. They all look like this:
class MyModelViewSet(viewset.ModelViewSet):
queryset = MyModel.objects.all()
serializer_class = MyModelSerializer
class MyModelSerializer(serializers.ModelSerializer):
class Meta:
model = MyModel
fields = '__all__'
I would like to make a generic Viewset and Serializer for that can simply take inn my model. I found this post to be helpful, but I would rather not have the model in the url. If possible, I would send the model with the router.register:
router.register('my_url', MyView(MyModel), basename='my-basename')
but this gives me an error:
AttributeError: This method is available only on the class, not on instances.
Any idea of how I can solve this?
question from:
https://stackoverflow.com/questions/65902304/create-generic-viewset-and-serializer-for-models-in-django-rest-framework 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…