Using model method in the django orm query

I have a model which has the model method called cancelled_date which returns date when a record from the models is cancelled how can i access this method in my query set to get the particular data.

class Record(models.Model):
    name = models.CharField(max_length=255)
    
    def cancellation_date(self):
        return cancelled_date

the function cancellation date returns the date the day record is cancelled

i want to filter the Cancellation date from Record model and do smething

Record.objects.filter()

You cannot use methods defined in the Model class, inside the .filter().

You can use annotation instead:

Record.objects.annotate(
    annotated_cancellation_date=datetime.today() #assign the desired value using orm funcions
).filter(
    annotated_cancellation_date=datetime.today()
)
Back to Top