[Django]-Django filter using select_related()

5👍

I don’t think the problem is related to selected_related. You are just trying to filter using a wrong lookup value. How about filtering with fixtureid__fixturematchday instead:

UserSelection.objects.select_related().filter(user=currentUserID, campaignno=currentCampaignNo, fixtureid__fixturematchday=18).order_by('fixtureid__fixturedate')[:1]

Since you want to get only a single object, why don’t you just use .first() to get an object instead of a queryset with one item:

campaignFixture = UserSelection.objects.select_related("fixtureid").filter(...).order_by(...).first()

2👍

According to your model, the relationship is the fixtureid

 UserSelection.objects.select_related().filter(user=currentUserID,campaignno=currentCampaignNo,fixtureid__fixturematchday=18)
👤Sayse

Leave a comment