1
再說說我的串行器看起來像這樣Django的REST框架序列化模型返回其STR表示
class EntitySerializer(serializers.ModelSerializer):
entity_instance = EntityInstanceSerializer(many=True)
tags = EntityTagSerializer(many=True)
class Meta:
model = Entity
fields = ('id', 'entity_instance', 'tags', 'note', 'source')
class EntityTagSerializer(serializers.ModelSerializer):
class Meta:
model = EntityTagLabel
fields = ('entity_tag_label',)
我的標籤模型是這樣的:
class EntityTag(models.Model):
entity_tag_label = models.CharField(max_length=255, unique=True)
def __str__(self):
return self.entity_tag_label
現在,這是好的,所有的,但我的回覆如下所示:
...
"tags": [
{
"entity_tag_label": "SENDER"
}
],
是否有可能爲我的標記序列化程序簡單地返回字符串r每個標籤的介紹?這樣,我只想有
"tags": [
"Sender",
],
爲例
啊!非常好,謝謝。出於好奇,是否有可能讓slugfield進行關係查詢?類似於像Entity.objects.filter(entity_instance__label ='somelable')這樣的查詢 –
不知道我是否正確理解你的問題。但是,如果你的意思是基於標籤過濾實體,那麼不,你不能用SlugRelatedField來實現。序列化程序的目的不是過濾你的查詢集。您可以閱讀關於DRF中的過濾[here](http://www.django-rest-framework.org/api-guide/filtering/#filtering-against-query-parameters) –