Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
407 views
in Technique[技术] by (71.8m points)

Create generic viewset and serializer for models in django rest framework

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...