Elisei Nicolae
Elisei Nicolae

Reputation: 352

How to get data from Database with a range?

So i want to make a range from a specific date to current date.

I make like this:

    context_data['newNotifications'] = Notification.objects.filter(
    emailStudent=request.user).filter(time=request.user.viewedNotifications).count()

But i want all records within the time values in range from request.user.viewedNotifications to current time.

Upvotes: 1

Views: 193

Answers (1)

jidicula
jidicula

Reputation: 3929

Have you seen the range filter?

Basically you could do:

import datetime
# ...

context_data['newNotifications'] = Notification.objects.filter(
    emailStudent=request.user).filter(
        time__range=(
            request.user.viewedNotifications,
            datetime.datetime.now()
        )
    ).count()

So your .filter() argument would be time__range=(start_date,end_date) where start_date comes from your request and end_date is datetime.datetime.now().

Upvotes: 1

Related Questions