我有2個模型叫做Car and Review。用戶可以爲汽車寫很多評論。 我可以過濾某輛車的所有評論。 我面臨的問題是,我只想在不到30天的時間內過濾屬於特定車輛的所有評論,並排除擁有該車的用戶。過濾小於30天的對象並排除特定對象的對象用戶
此問題支持我的問題Getting all items less than a month old,但我無法 篩選屬於特定車輛的所有評論在不到30天的時間內排除擁有該車的用戶。
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)
過濾所有審查一個特定的汽車
Car = Car.objects.get(pk=1)
Review = Review.objects.filter(car=Car)
過濾所有審查一個特定的汽車,但它檢索30天everycar我不希望所有的評論。我要檢索只在不到30天的審查特定的汽車,但不包括誰擁有汽車的用戶
Car = Car.objects.get(pk=1)
from datetime import datetime, timedelta
last_month = datetime.today() - timedelta(days=30)
Review = Review.objects.filter(car__in=car,review__gte=month)
我的問題是如何過濾在不到30對特定汽車所有評論並且不包括擁有該車的用戶的評論。
謝謝你對我的幫助
'Review'是您的型號名稱。確保你沒有壓倒這個意外。正如你在這裏做的那樣'Review = Review.objects.filter(car = Car)' –
好吧,我不明白這個錯誤SyntaxError:關鍵字arg後的非關鍵字arg Review.objects.filter(car__id = car.id,〜Q(user__id = car.user.id)) – JackRoster
對不起,我已經更新單步查詢'〜Q(user__id = car.user.id)'應該先到達在過濾器中。 –