[Answer]-Django filtering time

1👍

I think you inverted messages with message. Assuming message is your single object, we’ll filter messages like so:

messages = Message.objects.filter(in_response_to=message.in_response_to).filter(created__lt=message.created)

Check the docs for more comparison examples (lte means less than or equal but you could use lt, gt, gte and so on)

About the DateTime thing:

created = models.DateTimeField(default=datetime.datetime.now)

is one of the options you have (you have to import datetime). The other one is overriding your model’s save method:

class Message(models.Model):

    # other fields omitted to keep it clean
    created = models.DateTimeField(blank=True, null=True)        

    def save(self, *args, **kwargs):
        if not self.created:
            self.created = datetime.datetime.now()
        return super(Message, self).save(*args, **kwargs)

Leave a comment