5👍
Try using values_list, so it would be easier to check if an object exists:
dinner_entries = LotteryEntry.objects.filter(user=request.user).values_list('dinner__id', flat=True)
In your template, you can check like this:
{% if d.id in dinner_entries %}
0👍
It is a bit hard to answer your question without knowing your data model in a bit more detail, but I try to do so nevertheless:
Slow, but should work: Try to iterate on your lottery entries for each dinner, then look for your foreign key field in your LotteryEntry
record. If the dinner referenced matches the dinner record form the outer loop, then your’re there.
If you want a faster solution, then you need to prepare a dictionary based on your entries query set mapping the dinner records to the corresponding LotteryEntry
ones. Then you can use this map to look up the corresponding lottery entry by invoking the dictionary’s get
method. If there is no lottery entry, then you will just get None
.
- [Django]-Django url pattern matching
- [Django]-How to update a model instance in another model save method in django?