dfrojas
dfrojas

Reputation: 723

Getting an specific field of a model with a queryset

If i have this queryset:

z = Bets.objects.filter(match_id=request.POST['match']).filter(~Q(team_id=request.POST['team']))

How can i get the match_id or team_id?

If i do

print z

it shows me the unicode that i have in the model, i want for example

print z.match_id

but i getting 'QuerySet' object has no attribute 'match_id'. match_id is a ForeignKey

Upvotes: 0

Views: 59

Answers (1)

catavaran
catavaran

Reputation: 45565

You should loop through the queryset and print the field of found instances of Bet model:

for bet in z:
    print bet.match_id

If there is can be only one object that matches the criteria then you can use the get() or first() methods of queryset:

bet = z.first()
if bet:
    print bet.match_id

Upvotes: 2

Related Questions