Reputation: 4660
My model looks like this:
Person(model.Models):
name = model.CharField(max_length=32)
date_added = models.DateTimeField(auto_now_add=True)
Note(model.Models):
person = model.ForeignKey(Person)
action = model.CharField(max_length=32, default='call)
date_added = models.DateTimeField(auto_now_add=True)
owner = model.ForeignKey(User)
my view:
def get_working_persons(self):
relevant_actions = ['call', 'meeting', ...]
query = Q(action__in=relevant_note_actions) | Q(date_added__range=[fourteen_days_ago, now])
get_current_person_notes = Note.objects.filter(query).exclude(~Q(owner=request.user)).values('owner', 'action', 'person')
This returns a list of notes, where each person has all the notes listed, that are posted within the date_added__range. How do I get the last note for each person in that date range, that meets the other criterias?
Upvotes: 0
Views: 50
Reputation: 27503
Note.objects.filter(query).exclude(~Q(owner=request.user)).values('owner', 'action', 'person').first()
will return the top object of the queryset or
Note.objects.filter(query).exclude(~Q(owner=request.user)).values('owner', 'action', 'person').last()
will return the last object of the queryset
or latest by field name
Note.objects.filter(query).exclude(~Q(owner=request.user)).values('owner', 'action', 'person').latest('field_name')
Upvotes: 1