I'm using Django and Python 3.7. I have the below two models ...
class Article(models.Model):
...
publisher = models.ForeignKey(Publisher, on_delete=models.CASCADE, related_name="articles",)
created_on = models.DateTimeField(default=datetime.now)
class WebPageStat(models.Model):
...
publisher = models.ForeignKey(Publisher, on_delete=models.CASCADE, related_name="stats", )
elapsed_time_in_seconds = models.FloatField(default=0)
score = models.BigIntegerField(default=0)
class Publisher(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
I want to write a Django ORM query where given a publisher and an elapsed time in seconds (a WebPageStat record), I find all articles whose "created_on" date is not older than the elapsed time in seconds. Many have suggested using "timedelta," in other posts, but that doesn't seem to be working for me here ...
Article.objects.filter(created_on__lte=datetime.now(timezone.utc) - timedelta(hours=0, minutes=0, seconds=publisher__stats__elapsed_time_in_seconds))
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'publisher__stats__elapsed_time_in_seconds' is not defined
Can I use timedelta with SQL column logic? Otherwise how do I do this?