[Answered ]-Django model auto update status when the medicine is expired

1👍

Create crontab for one time in a day as:

#this is example for each day 00:00 sending get request to you api
0 0 * * * /usr/bin/curl --silent http://example.com/your_views_url/ &>/dev/null

This is good and easy to understand cronjob generator:

https://crontab.guru/#0_0_*_*_*

Cronjob will trigger a view, which will search for a medicine, and change its status:

urls.py:

path("your_views_url", views.your_view_name_here),

views.py:

from django.http import HttpResponse
from datetime import datetime

def your_view_name_here(request):
    now = datetime.today()
    
    #this will return a queryset of all medecine which you need to change status
    all_target_medecine = Medicine.objects.filter(medstatus = "Active", expirationDate__lt = now)

    for medecine in all_target_medecine:
        medecine.medstatus = "Expired"
        medecine.save()
    return HttpResponse("Ok")

Leave a comment