Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get all objects with certain interval of time in django

I want to get all objects which has time to live next 5 seconds with django python ORM. I’m trying in a following way and I don’t know why that is not working or what i’m doing wrong…

queryset.py

def ttl_expire_list(self):
        query = self.filter(is_remove=False,ttl__range=[timezone.now() + timedelta(seconds=5), timezone.now()]).order_by("-ttl")
        # query = self.filter(is_remove=False).order_by("-ttl")
        return query'

models.py

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class Notification(models.Model):

    sender = models.CharField(_("Sender"), max_length=100,null=True,blank=True)

    receiver = models.CharField(_("Receiver"), max_length=100,null=True,blank=True)
    message = models.TextField(_("Message"),null=True,blank=True)

    is_read = models.BooleanField(_("Read") ,default=False,null=True,blank=True)
    ttl = models.DateTimeField(_("Time to live"),null=True,blank=True) 
    create_time = models.DateTimeField(_("Created Time"), default = timezone.now)

Solution

def ttl_expire_list(self):
        print("curen",timezone.now()) 
        print("imte :",timezone.now() + timedelta(seconds=5))
        query = self.filter(is_remove=False,ttl__range=(timezone.now() , timezone.now()+timedelta(seconds=50))).order_by("-ttl")
        return query

>Solution :

First argument of range should be start date and then end date just swap the arguments:

 ttl__range=[timezone.now(), timezone.now() + timedelta(seconds=5)]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading