Reputation: 375
I know the normal way to filter item in a QuerySet looks something like this:
class GameAPIListView(ListAPIView):
serializer_class = GameSerializer
def get_queryset(self):
data = Game.objects.filter(name="Football")
return data
What if I have a model that looks like this:
{
"category_id": {
"parent_id": {
"name": "Sports",
"category_id": "bf14aab0-dc76-4145-a130-246784d1efc1",
"parent_id": null
},
"name": "Football",
"category_id": "b6e59886-e834-4612-abc7-c32bd0e2af35"
},
"name": "Play Football",
}
{
"category_id": {
"parent_id": {
"name": "Sports",
"category_id": "bf14aab0-dc76-4145-a130-246784d1efc1",
"parent_id": null
},
"name": "Basketball",
"category_id": "b6e59886-e834-4612-abc7-c32bd0e2af35"
},
"name": "Play Basketall",
}
{
"category_id": {
"parent_id": {
"name": "Poker",
"category_id": "bf14aab0-dc76-4145-a130-246784d1efc1",
"parent_id": null
},
"name": "blackjack",
"category_id": "b6e59886-e834-4612-abc7-c32bd0e2af35"
},
"name": "Play blackjack",
}
And I would like to get all the items whose name of its parent_id is Sports. In this case I will get Football and Basketball.
I tried something like
data = Game.objects.filter(category_id.parent_id.name = "Sports")
But it did not work. Can someone show me the correct way to do this? Thanks a lot!
Upvotes: 0
Views: 83
Reputation: 83
If you add model code, you can get a more exact answer.
Why don't you try this.
data = Game.objects.filter(category__parent__name = "Sports")
Upvotes: 1