0

I am setting a web server based on Django Framework with PostgreSQL. I need to update the models in my Django app every hour and I am wondering if is it better way to update the database from another script file using python and psycopg2 or using script that directly connects to Django models and update them using Django API for models? I use this Database only in Django web server.

Summarizing... What is better logic?

  1. update_db.py that contains:

    • connect to database via psycopg2, update the tables
  2. update_db.py that contains:

    • connect to Django app models, update them using Django API for models

I will run script every hour using Cron, and the Django Project is already connected to that specific database and it works fine. I am just asking for better performance and better way in logic sense. Data won't be updated very often but it would have big jsons.

3
  • 1
    you can be more pythonic if you use celery perioidic task docs.celeryproject.org/en/latest/userguide/periodic-tasks.html number 2 is better as they are handling connection pools and disconnect Commented Jan 16, 2019 at 7:31
  • @YugandharChaudhari using a complex and costly system when a simple, well-known and stable one exists is definitly not pythonic. Commented Jan 16, 2019 at 9:13
  • @brunodesthuilliers I learnt something thank you. Commented Jan 16, 2019 at 12:46

1 Answer 1

1

Unless you have very compelling reasons to do otherwise, the obvious solution here is to use a custom management command and the ORM - your code will be at the obvious place (for someone else having to work on the project), can be tested as part of your whole project's test suite, and won't require having to mentally "translate" from raw SQL to Django ORM code (not to mention that you'll have more chance to catch a mismatch between your Django models and your script's code when your schema changes - from experience, raw sql scripts that aren't under test are usually forgotten when doing a schema migration).

FWIW, optimizing "for performances" doesn't really makes sense in your case, unless your updates are long and complex and do block access to the site - but even then the overhead of the ORM is certainly not going to be the main bottleneck (just make sure you use properly).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.