使用django rest-farmework来实现API,有一个问题一直无法解决:如何过滤关联的表?,具体代码如下:
models.py
class Category(models.Model):
name = models.CharField(max_length=30)
amount = models.IntegerField()
class Source(models.Model):
name = models.CharField(max_length=50)
rss_link = models.URLField()
amount = models.IntegerField()
# ForeignKey
category = models.ForeignKey(Category)
views.py
class CategoryListView(APIView):
def get(self, request):
category = Category.objects.all()
serializers = CategorySerializers(category, many=True)
return Response(serializers.data)
serializers.py
class SourceSerializers(serializers.ModelSerializer):
class Meta:
model = Source
fields = ("id","name","amount")
class CategorySerializers(serializers.ModelSerializer):
source_set = SourceSerializers(many=True, read_only=True)
class Meta:
model = Category
fields = ("id","name","amount","source")
程序运行结果:
[
{
"id": 1,
"name": "study",
"amount": "0",
"source": [
{
"id": 34,
"name": "java",
"amount": "0"
},
{
"id": 35,
"name": "python",
"amount": "0"
},
{
"id": 36,
"name": "css",
"amount": "2"
}
]
}
]
现在我只需要查询" source" "金额" 0个数据,我试着写这个代码:
serializers.py
class SourceSerializers(serializers.ModelSerializer):
class Meta:
model = Source
fields = ("id","name","amount")
class CategorySerializers(serializers.ModelSerializer):
#Modified the following code
source_set = SourceSerializers(Source.objects.filter(amount=0),many=True, read_only=True)
class Meta:
model = Category
fields = ("id","name","amount","source")
修改了以下代码:
source_set = SourceSerializers(many=True, read_only=True)
到
source_set = SourceSerializers(Source.objects.filter(amount=0),many=True, read_only=True)
但结果是一样的,我希望结果是:
[
{
"id": 1,
"name": "study",
"amount": "0",
"source": [
{
"id": 34,
"name": "java",
"amount": "0"
},
{
"id": 35,
"name": "python",
"amount": "0"
}
]
}
]
提前致谢。
答案 0 :(得分:1)
稍微更改序列化程序。您可以拥有一个SourceWithCategorySerializer来序列化相关的Category对象。然后,您可以根据金额查询Source对象,并将该QuerySet传递给序列化程序。
您的观点:
class CategoryListView(APIView):
def get(self, request):
sources = Source.objects.filter(amount=0)
serializers = SourceWithCategorySerializers(sources, many=True)
return Response(serializers.data)
序列化器:
class SourceWithCategorySerializers(serializers.ModelSerializer):
category = CategorySerializer(read_only=True)
class Meta:
model = Source
fields = ("id","name","amount", "category")
class CategorySerializers(serializers.ModelSerializer):
class Meta:
model = Category
fields = ("id","name","amount")
答案 1 :(得分:1)
您可以使用SerializerMethodField
:
class CategorySerializer(serializers.ModelSerializer):
source_set = serializers.SerializerMethodField('get_sources')
def get_sources(self, category):
sources = category.source_set.filter(amount=0)
return SourceSerializer(instance=sources, many=True).data
class Meta:
model = Category
fields = ("id", "name", "amount", "source")
我删除了复数'从你的班级名称,因为他们给我一个痒;)