How to run a function in django within a specified time?

Viewed 53

views.py

from django.shortcuts import render, redirect
from .models import Product
from .tasks import set_firsat_urunu
from datetime import datetime
# Create your views here.

def index(request, id):
    product = Product.objects.get(id=id)
    
    if request.method == 'POST':
        product.name = request.POST.get('name')
        product.description = request.POST.get('desc')
        product.deadline_at = request.POST.get('deadline_at')
        product.firsat_urunu = [False, True][bool(request.POST.get('check'))]

        product.save()
        set_firsat_urunu.apply_async(args=[id],eta=datetime(2021,8,17,0,10))
        return redirect(request.META.get('HTTP_REFERER'))

    return render(request, 'product.html', {'product':product})

tasks.py

from celery import shared_task,current_task
from .models import Product

@shared_task(bind=True)
def set_firsat_urunu(id=1):
    p = Product.objects.get(id=id)
    if p.firsat_urunu == True:
        p.firsat_urunu = False
    return 'done'

I want to run a task with celery and make changes to a record in the database when the time I specify comes. When I run the code blocks I mentioned above, although the celery states that the relevant function has been received, I can't see anything in the django admin Celery Result section and at the same time, the celery task does not work even if the specified time has passed. Also, when I give apply_async ' args=[id] with @shared_task(bind=True), set_firsat_urunu gives an error because you gave 2 parameters but it takes only 1.

0 Answers
Related