0

I need to create a standalone script which accesses the database, fetches data from a table and processes it and stores it into another table. I am also using django-rq in order to run this script.

  1. Where should place this script in django project structure?
  2. Without using views.py, how should I run this script using django-rq?
blackmamba
  • 1,952
  • 11
  • 34
  • 59

3 Answers3

0

If I understand your case correctly, I would use custom management commands https://docs.djangoproject.com/en/1.7/howto/custom-management-commands/

iperelivskiy
  • 3,523
  • 2
  • 18
  • 19
0

In one of your views import the scripts' functions and django-rq and continue with your processing in the view.

BernardNdegwa
  • 96
  • 1
  • 4
0

I just worked this same issue, One added variable was that I wanted to run a job every hour, so in addition to Django-RQ I am also using RQ-Scheduler.

In this approach, you schedule a function call in the job you create

scheduler.schedule(
    scheduled_time=datetime.utcnow(), # Time for first execution, in UTC timezone
    func=func,                     # Function to be queued
    args=[arg1, arg2],             # Arguments passed into function when executed
    kwargs={'foo': 'bar'},         # Keyword arguments passed into function when executed
    interval=60,                   # Time before the function is called again, in seconds
    repeat=10                      # Repeat this number of times (None means repeat forever)
)

I created a module my_helpers.py in the root of my Django Project which has functions that do the work I want and which scheduled the task as I needed. Then in a separate shell python manage.py shell I import the helpers and run my function to scheduled the task.

I hope that helps, its working for me.

Patrick
  • 2,044
  • 1
  • 25
  • 44