1👍
✅
When you design your database structure you should make sure you don’t repeat the same information in multiple places – i.e. that the database in normalized. Get rid of the next_meeting
field all together, you can still get the information with the right query:
Get the most recent meeting for customer with id customer_id
:
Meeting.objects.filter(customer=customer_id).order_by('date').first()
Add most recent meeting date to every user in a queryset:
Customer.objects.annotate(next_meeting=Min('meeting__date'))
This assumes there are no past meetings in the database – because your solution also required that is the case. If it is not you would obviously need one more filter, to filter out meetings in the past.
Source:stackexchange.com