Reputation: 1783
I have two models
Product
id:
delivery_date: DateTimeField
contract: FK
Contract
id:
safety_days: IntegerField
I need to get the products to deliver in the next X days. This X are defined in the contract as safety_days.
products = Product.objects.filter(
Q(delivery_date__lte=(datetime.datetime.now() + timedelta(days=contract__safety_days)))
)
However,I am not able to do:
days = contract__safety_days
I do not know how I can reference the object itself inside the query to do this dynamically.
Best regards, Ruben Barros
Upvotes: 3
Views: 222
Reputation: 977
You need to use F expressions for this query. Your query would look the following way:
products = Product.objects.filter(
Q(delivery_date__lte=(datetime.datetime.now() + timedelta(days=F('contract__safety_days'))))
)
Upvotes: 4