You can do it like this (using Python dynamics):
def create_serializer(target_class, fields): class ModelSerializer(serializers.ModelSerializer): class Meta: model = target_class fields = fields return ModelSerializer
Thus, a path serializer can be created using this approach:
TrackSerializer = create_serializer(Track, ('album__name',))
Also, be careful with ('album__name') , which is a string expression in brackets, not a tuple as you probably intended. To declare it as a tuple, add it after the comma, for example:
fields = ('album__name',)
source share