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
records = Record.objects.all()
for record in records:
if record.cancellation_date() == timezone.now()
I am able to access in like this but is there a way that i could filter only records cancellation_date which are set to today's date
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()
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.