How to serialize a queryset from an unrelated model as a nested serializer?

旧城冷巷雨未停 提交于 2021-02-17 09:41:30

问题


I'm trying to add a nested serializer to an existing serializer based on some criteria of the parent model, not a Foreign key. The use case is to return a 'Research' object with an array of 'ResearchTemplate' objects that are identified by filtering on a Postgres ArrayField.

Models


class Research(TimeStampedModel):
    category = models.CharField(max_length=100, choices=RESEARCH_TEMPLATE_CATEGORIES, default='quote')
    body = models.CharField(max_length=1000, blank=True, default='') #The body of text comprising the nugget
    additionaldata = JSONField(null=True) # all the varying values to be merged into a wrapper

    def __str__(self):
        return self.body

    class Meta:
        ordering = ('created',)

class ResearchTemplate(TimeStampedModel):
    template = models.TextField(blank=True, default='')
    category = models.CharField(max_length=100, choices=RESEARCH_TEMPLATE_CATEGORIES, default='quote')
    mergefields = ArrayField(models.CharField(max_length=200), blank=True)

    def save(self, *args, **kwargs):
        merges = re.findall("{{(.*?)}}", self.template) #get all the template names from within the mustaches
        self.mergefields = list(set(merges)) #TODO: Make Unique
        super(TimeStampedModel, self).save(*args, **kwargs)

    def __str__(self):
        return self.wrapper

    class Meta:
        ordering = ('created',)

Serializers


class ResearchSerializer(serializers.ModelSerializer):
        templates = ResearchTemplateSerializer(many=True)

        class Meta:
            model = Research
            fields = ('id', 'created', 'speaker', 'body', 'templates')

class ResearchTemplateSerializer(serializers.RelatedField):
        def get_queryset(self, values):
            return ResearchTemplate.objects.filter(mergefields__contained_by=['django']) #This must an array of keys from the Research object's JSON field

        class Meta:
            model = ResearchTemplate
            fields = ('id', 'template')

I've been able to nest serializers when there is a foreign key mapping them, however unable to do so with a custom queryset. Perhaps I'm not thinking about this properly, and I require some form of 'relationship' field on the Research model.

How can I nest a serialized list of all rows that are returned from a filter with values specified from the parent model?


回答1:


You can use DRF's SerializerMethodField.

Define your ResearchTemplateSerializer as a normal ModelSerializer, not as a RelatedField.

Then replace your ResearchSerializer with this:

class ResearchSerializer(serializers.ModelSerializer):
    templates = serializers.SerializerMethodField()

    class Meta:
        model = Research
        fields = ('id', 'created', 'speaker', 'body', 'templates')

    def get_templates(self, obj):
        values = obj.get_values() # whatever your filter values are. obj is the Research instance
        templates = ResearchTemplate.objects.filter(mergefields__contained_by=values) # Or whatever queryset filter
        return ResearchTemplateSerializer(templates, many=True).data


来源:https://stackoverflow.com/questions/42775784/how-to-serialize-a-queryset-from-an-unrelated-model-as-a-nested-serializer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!