我正尝试根据父模型的某些条件(而不是外键)向现有序列化程序添加嵌套序列化程序。用例是返回一个“Research”对象,该对象带有一个“ResearchTemplate”对象数组,这些对象通过在Postgres ArrayField上筛选来标识。
型号
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',)
序列化程序
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')
当有一个外键Map序列化程序时,我已经能够嵌套它们,但是我不能用自定义查询集这样做。也许我没有正确地考虑这个问题,我需要在研究模型上有某种形式的“关系”字段。
如何嵌套从具有从父模型指定的值的筛选器返回的所有行的序列化列表?
1条答案
按热度按时间8dtrkrch1#
您可以使用DRF的
SerializerMethodField
。将您的
ResearchTemplateSerializer
定义为一般的ModelSerializer,而不是RelatedField
。然后将您的
ResearchSerializer
替换为: