How to create a generic serializer with a dynamic model in Meta

0 votes

When i create a Serializer in django-rest0-framework, based on a ModelSerializer, i will have to pass the model in the Meta class:

class ClientSerializer(ModelSerializer):
    class Meta:
        model = Client

I want to create a general serializer which, based on the URL, includes the model dynamically.

My setup thusfar includes the urls.py and the viewset:

urls.py:

 url(r'^api/v1/general/(?P<model>\w+)', kernel_api_views.GeneralViewSet.as_view({'get':'list'}))

and views.py:

class GeneralViewSet(viewsets.ModelViewSet):

     def get_queryset(self):
            # Dynamically get the model class from myapp.models
            queryset = getattr(myapp.models, model).objects.all()
            return queryset

     def get_serializer_class(self):
         return getattr(myapp.serializers, self.kwargs['model']+'Serializer')
Aug 5, 2020 in Python by kartik
• 37,510 points
3,114 views

1 answer to this question.

0 votes

Hello @kartik,

You can do that by following:

serializers.py

class GeneralSerializer(serializers.ModelSerializer):

    class Meta:
        model = None

views.py

class GeneralViewSet(viewsets.ModelViewSet):

     def get_queryset(self):
         model = self.kwargs.get('model')
         return model.objects.all()           

     def get_serializer_class(self):
         GeneralSerializer.Meta.model = self.kwargs.get('model')
         return GeneralSerializer  

In serializers.py, we define a GeneralSerializer having model in Meta as None. We'll override the model value at the time of calling get_serializer_class().

Then in our views.py file, we define a GeneralViewSet with get_queryset() and get_serializer_class() overridden.

In get_queryset(), we obtain the value of the model from kwargs and return that queryset.

Hope it helps!!
Thank you!!

answered Aug 5, 2020 by Niroj
• 82,880 points

Related Questions In Python

0 votes
1 answer

How to create a unicode string in python with the string eg: This is a string?

Hey, @Roshni, It is very simple to execute, ...READ MORE

answered Jun 23, 2020 in Python by Gitika
• 65,910 points
527 views
0 votes
1 answer

How to create and read from a temporary file in Python?

Hi, there is a very simple solution ...READ MORE

answered Jan 29, 2019 in Python by Nymeria
• 3,560 points
1,779 views
0 votes
1 answer

How to create a GUID/UUID in Python

The uuid module, in Python 2.5 and ...READ MORE

answered Jan 29, 2019 in Python by SDeb
• 13,300 points
1,540 views
0 votes
1 answer

How to zip with a list output in Python instead of a tuple output?

Good question - Considering that you are ...READ MORE

answered Feb 7, 2019 in Python by Nymeria
• 3,560 points
1,404 views
0 votes
1 answer

How to temporarily disable a foreign key constraint in MySQL?

Hello @kartik, To turn off foreign key constraint ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,880 points
2,084 views
0 votes
1 answer

How do I use Django templates without the rest of Django?

Hello @kartik, Let's say you have this important ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,880 points
1,230 views
+1 vote
1 answer

How to create a model dynamically just for testing in djanjo?

Hello @kartik, The basic idea is, make your tests an ...READ MORE

answered Jun 29, 2020 in Python by Niroj
• 82,880 points
925 views
0 votes
1 answer

How to create a zip archive of a directory in Python?

Hello @kartik, The easiest way is to use shutil.make_archive. ...READ MORE

answered May 11, 2020 in Python by Niroj
• 82,880 points
440 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP