Reputation: 223
I am new to Python Django and I want to make a query
class A(models.Model):
foo = models.IntegerField(default=0)
from_time = models.DateTimeField('start time')
to_time = models.DateTimeField('end time')
class B(models.Model):
model_a = models.ForeignKey(A, on_delete=models.PROTECT, related_name='model_a')
model_c = models.ForeignKey(C, on_delete=models.PROTECT, related_name='model_c')
class C(models.Model):
id = models.IntegerField(default=0)
name = models.CharField(max_length = 20)
I want to make a query on class B where the current time is in between model_a.from_time and model_a.to_time. for that, I tried
B.objects.filter(model_c=model_c_id, model_a.to_time__gte=start_date, model_a.to_time__lte=end_date)
it gives me syntax error SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
Upvotes: 1
Views: 470
Reputation: 15738
You are using wrong syntax to span relationship
B.objects.filter(model_c__id=model_c_id, model_a__to_time__gte=start_date, model_a__to_time__lte=end_date)
Upvotes: 2