Reputation: 111
Using django rest-farmework to implement the API,There is a problem has been unable to solve: How to filter the associated tables?, the specific code is as follows:
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")
Program running results:
[
{
"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"
}
]
}
]
Now I only need to query "source" of the "amount" of 0 data, I tried to write this code:
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")
Modified the following code:
source_set = SourceSerializers(many=True, read_only=True)
to
source_set = SourceSerializers(Source.objects.filter(amount=0),many=True, read_only=True)
But the result is the same, I hope the result is:
[
{
"id": 1,
"name": "study",
"amount": "0",
"source": [
{
"id": 34,
"name": "java",
"amount": "0"
},
{
"id": 35,
"name": "python",
"amount": "0"
}
]
}
]
Thanks in advance.
Upvotes: 0
Views: 396
Reputation: 73470
You could use a 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")
I removed the plural 's' from your class names because they were giving me an itch ;)
Upvotes: 1
Reputation: 1292
Change your Serializers a bit. You can have a SourceWithCategorySerializer that serializes the related Category object. Then, you are able to query Source objects based on amount and pass that QuerySet to your serializer.
Your view:
class CategoryListView(APIView):
def get(self, request):
sources = Source.objects.filter(amount=0)
serializers = SourceWithCategorySerializers(sources, many=True)
return Response(serializers.data)
The serializers:
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")
Upvotes: 1