0
I am having a logical problem trying to perform a data filtering on Django. According to the code below, I am trying to filter an object inside my database:
class UserActivityQuerySet(models.query.QuerySet):
def today(self):
now = timezone.now()
today_starts = timezone.make_aware(datetime.combine(now,time.min))
today_ends = timezone.make_aware(datetime.combine(now,time.max))
return self.filter(timestamp__gte=today_ends,timestamp__lte=today_starts)
def current(self,user=None):
if user is None:
return self
return self.filter(user=user).order_by("-timestamp").first()
So, for the code in question I selected a timestamp filtering by gte and Lte to take a time frame for the start and end of the day respectively.
Thus, how to solve the problem of no object being returned?
I don’t think you’re calling that part of the view you shared. You should use that queryset inside a
model.Manager
, not directly as you are doing. That way should return an error.– afonso
Yes. They really are outputs from different points. I’m just showing that there are records made in the last 24 hours and that the fault is not in the bank
– Wanghley
Edit the question and add the relevant part of models.py and views.py to help you out.
– afonso
I would do it as a model property
– Davi Wesley