I have a model with a custom method. Here’s the example:
class MyModel(models)
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=50)
nickname = models.CharField(max_length=50)
time_created = models.DateTimeField(auto_now_add=True)
def sample_method(self)
right_now = datetime.now()
created_time = self.time_created
time2compare = timedelta(minutes=3)
timeout = True
if(right_now - created_time) > time2compare:
timeout = False
return timeout
What I’m trying to do is do something like this (i know this doesn’t work)
queryset = MyModel.objects.filter(timeout=False)
How could i solve this problem? Thanks!
You cannot use a custom method on your model for filtering, because the filters will be resolved to saw sql, which will not be possible with some python code. Nonetheless the following should solve your problem: