I am trying to get one item in a query set but I always get the following error in my view:
Tip matching query does not exist.
Here is my view code:
tip = None
if not hole.tip_set.all():
tip = hole.tip_set.create(content="New")
else:
print hole.tip_set.all()
tip = hole.tip_set.get(pk=1)
When I do print hole.tip_set.all(), the following prints in my console: [<Tip: Tip object>]
But, when I try to “get pk 1”, I get the error Tip matching query does not exist. This doesn’t make sense to me at all. There is clearly an item in the hole’s tip_set. Yet, when I try to retrieve it, it says no tips match the query.
That’s because the object’s PK is not
1.You’re looking for
hole.tip_set.all()[0].