[Answer]-Storing repeated Complex Calculations and serving through tastypie api

1πŸ‘

βœ…

1. Make database models for cached data.

Django docs

2. Make a manage.py command that does calculations and stores it to model from 1.

project/calcucaltion_app/management/commands/calculate_data.py

from django.core.management.base import BaseCommand
from calculation_app.models import my_model

class Command(BaseCommand):
    args = "No arguments needed"
    help = "This script calculates financial data and caches it"

    def handle(self, *args, **options):
        # TODO code here

Remember to put empty __init__.py in management and commands folders. After this you can call you script with ./manage.py calculate_data and the script will have access to django ORM. See Django documentation for more information.

3. Make a cron job that fires every 3 minutes and calls the command from 2.

Run crontab -e on your linux server and add:

*/3 * * * * /path/to/django/manage.py calculate_data

If you have set up a virtualenv you might need to make adjustments.

4. Make a tastypie model that handles fetching data from the model in 1.

Tastypie docs

Leave a comment