Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating QuerySet object from last 7 days

Tags:

posts = Post.objects.filter(author=member.user, xyz=xyz_id, pub_date >= datetime.datetime.now()-7)

I want to extract all posts by those requires of author and xyz which will be from last 7 days. Results only from last 7 days. I ofc know that this is wrong but I do not have idea how to code it.

like image 462
user1403568 Avatar asked Jun 11 '12 21:06

user1403568


People also ask

What is a Queryset object?

A QuerySet is a collection of data from a database. A QuerySet is built up as a list of objects. QuerySets makes it easier to get the data you actually need, by allowing you to filter and order the data.

Is Django Queryset lazy?

This is because a Django QuerySet is a lazy object. It contains all of the information it needs to populate itself from the database, but will not actually do so until the information is needed.

What does a Queryset return?

Returns a QuerySet that returns dictionaries, rather than model instances, when used as an iterable. Each of those dictionaries represents an object, with the keys corresponding to the attribute names of model objects.

What does Queryset filter return?

The filter() method is used to filter you search, and allows you to return only the rows that matches the search term.


1 Answers

from datetime import datetime, timedelta  posts = Post.objects.filter(author=member.user, xyz=xzy_id, pub_date__gte=datetime.now()-timedelta(days=7)) 
like image 55
Chris Pratt Avatar answered Sep 21 '22 07:09

Chris Pratt