django - Filtering objects less than 30 days and excluding object's user for a particular object -


i have 2 models called car , review . user can write many reviews car. able filter reviews car . issue i'm facing , want filter reviews belong particular car in less 30 days , excluding user own car.

this question support question getting items less month old unable filter reviews belong particular car in less 30 days , excluding user own car.

class car(models.model):     user = models.foreignkey(user)     name = models.charfield(max_length=11)   class review(models.model):     created = models.datetimefield(auto_now_add=true)     user = models.foreignkey(user)     review = models.textfield()     car = models.foreignkey(car) 

filter review particular car

car = car.objects.get(pk=1) review = review.objects.filter(car=car) 

filter review particular car retrieves reviews everycar in 30 days don't want . want retrieve reviews in less 30 days particular car , excluding user own car

car = car.objects.get(pk=1) datetime import datetime, timedelta last_month = datetime.today() - timedelta(days=30) review = review.objects.filter(car__in=car,review__gte=month) 

my question how can filter reviews particular car in less 30 days , not including reviews of user own car .

thank helping me

first of need filter on created field rather on review field lookup older entries:

from django.db import q datetime import datetime, timedelta  car = car.objects.get(pk=1) threshold = datetime.now() - timedelta(days=30)  # filtering step step give better understanding reviews = review.objects.filter(car__id=car.id) # filter car first reviews = reviews.filter(created__gte=threshold) # filter datetime reviews = reviews.filter(~q(user__id=car.user.id)) # exclude user reviews owns car  # filtering in single step reviews = review.objects.filter(     ~q(user__id=car.user.id), car__id=car.id, created__gte=threshold) 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -