Update value certain time later using django appscheduler

I want to update a value every day at a certain time later.

I wrote my models and views look like

models.py

class InvestmentRequest(models.Model):
    user = models.OneToOneField(User, related_name=“investment_request”, on_delete=models.CASCADE)
    profit = models.DecimalField(max_digits=15, decimal_places=2)
    created_at = models.DateTimeField(auto_now_add=True)

def __str__(self):
    return f"{self.user.username}-{self.amount}"

def increase_balance(self, profit ):
    self.profit += decimal.Decimal(profit )
    self.save()

def save(self, *args, **kwargs):
    super().save(*args, **kwargs)

views.py

from apscheduler.schedulers.background import BackgroundScheduler

def roi_profit(request):
    investment = InvestmentRequest.objects.get(user=request.user)
    amount = 15
    investment .increase_balance(amount)
    return …

scheduler = BackgroundScheduler()
job = None

def tick():
    print(‘One tick!’)
    # roi_profit() --- any way like this


def start_job():
    global job
    job = scheduler.add_job(tick, 'interval', seconds=3)
    try:
        scheduler.start()
    except:
        pass

I used here “appscheduler” library because my requirement is minimal, it’s not a complex configuration like celery. Is there any way to run ‘roi_profit’ views at a certain time every day? Here ‘tick’ function runs very smoothly because it is a normal function. But I can’t call the views function because it’s a required request. And without request, I can’t get any value from request.user. It’s necessary. So this way how I can update my model value at a certain time later every day. Over a couple of days I’m struggling with it but couldn’t find the exact solution.

Any suggestions will be appreciated. Thanks.

Back to Top