Django filtering of timestamp transactions

Asked

Viewed 68 times

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.

  • 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

  • Edit the question and add the relevant part of models.py and views.py to help you out.

  • I would do it as a model property

1 answer

1


So, to solve the problem presented and after several analyses and tests, I made only one modification and the filtering by timestamp worked without major problems. In the filtering code, just change the `Return`, separating the filters, as follows the function changed in the `models.py file`:

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_start).filter(timestamp__lte=today_end)

Already, for the view of the query, it was changed inside the file views.py the function get class UserActivityView(View) in order to present the number of events by means of the following code:

 def get(self, request, *args, **kwargs):
        print(UserActivity.objects.all().count())
        print(UserActivity.objects.all().today().count())

        all_activity  =UserActivity.objects.all().today().recent()

        context = {
            "all_activity": all_activity,
        }
        return render(request, "timeclock/users-activity-view.html", context)

Being the function recent() only one filtering to organize the result by timestamp through the following filtering: .filter("-timestamp") and the class call Useractivity the model that was created within models.py to meet demand.

For more information, go to the repository of such a project on Github by clicking here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.