Forums

Question about Scheduler

Hi, beginner programmer here:

I am using django to build a web app called watergunwars. In my watergunwars directory, I have my models.py module, as well as a file called eliminate.py that I would like to run once per day.

# from eliminate.py:
from watergunwars.models import UserProfile

for agent in UserProfile.agents:
    agent.kill_count += 1
    agent.save()

In scheduler, I set the path to /home/kaliqwang/CentennialWGW/watergunwars/eliminate.py, but the log returns the following error:

Traceback (most recent call last):
  File "/home/kaliqwang/CentennialWGW/watergunwars/eliminate.py", line 1, in <module>
    from watergunwars.models import UserProfile
ImportError: No module named watergunwars.models

2016-04-09 00:09:07 -- Completed task, took 0.00 seconds, return code was 1.

Can someone help me figure out what I'm doing wrong?

If you want to run some Django code from outside your Django website, then it's best to use a Django "management command". This is because Django does all kinds of clever setup stuff (including system path mangling and database connection setup), and if you just run your code directly then that won't happen.

Here are the appropriate docs.

Alright, I have set up my command, called "eliminate.py" and have verified it works using python manage.py eliminate in the bash console. The question is, how can I use the scheduler to run this command once per day?

Nevermind, figured it out!

https://www.stavros.io/posts/standalone-django-scripts-definitive-guide/

             / ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄\    
<<<<<<:>~  <   Yay!          |   
             \_________/