How to update Django models from another app? - python

I have a running Django server using MySQL.I want to modify its models from another python program.
I came up with a few ideas.
Directly insert data into MySQL (will this reflect on model objects?)
Create a REST interface
I'm unfamiliar with Django's ORM, what is the right approach?

Related

Update the PostgreSQL Database every hour via python script or using django models?

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?
update_db.py that contains:
connect to database via psycopg2, update the tables
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.
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).

Whats the way to get existing Mongo db as Django Model? [duplicate]

I have a few databases in MongoDB that I want to create models for dynamically, since there are many databases and I cannot do it manually. Questions:
What should my models.py look like? (Does inspectdb work with mongodb databases or only SQL based dbs?)
Since the database models are created dynamically, how do I code the serializer class to return the dynamic fields?
Thanks in advance.
Django supports an object-relational mapper, that is aimed at traditional relational databases. While there are a number of mongodb packages for Django, none of them support inspectdb to construct your models. Either way, inspectdb is a kludge designed as a one of process to help a one-of migratation away from a legacy system, i.e. you'd build your models.py file once and never run inspectdb again. This is not what you want to do, as you seem to want dynamic models that can be added or altered at runtime.
On the bright side, Django MongoDB Engine has some support for arbitrary embedded models within pre-defined models. But even then they don't seem too supportive of it:
As you can see, generic embedded models add a lot of overhead that bloats up your data records. If you want to use them anyway, here’s how you’d do it...
In summary, try to build your models as best you can to actually match your requirements. If you know nothing about your models ahead of production, then perhaps Django isn't the right solution for you.

Making Flask connection to existing Postgres tables based on Django models

I am developing a Flask app that queries on an existing Postgres DB. This DB had been created with Django models. Do i need to reverse engineer so my models would be compatible with SQLAlchemy or will that already be. Is raw queries the way to go? I don't want to create new DBs just query and update some values in the existing ones, that had been created in Postgres using my django models.
Go check out sqlalchemy's AMAZING automap feature: http://docs.sqlalchemy.org/en/latest/orm/extensions/automap.html
Used it once in my personal project for similar purpose, it works great.

Publishing django views with already existing db tables

I have experience programming in python, but am new to Django. I recently went through the tutorial and eveything made sense, however I am wondering if I can create views from DB tables that already exists without having to create models. Or, if this isn't possible is there a way to create models to generate an ORM with a DB table that already exists?
Basically I have Aggregate metrics in data warehouse tables that I would like to publish to a web page in real time. Is Django the right tool for this? Thanks.
Use ./manage.py inspectdb to create models from an existing database.

Django Reusing models

I have Django application with 30+ models. I want to write an application that can take a snapshot of the data in some of the models. I want to write the models once and reuse them in each application so that if I maintain it in one place, the only difference being that when I call python manage.py syncdb the same table are created with different table prefixes.
Is there any way to do this?
This is exactly where the reusable app principle comes into play.
(as explained at the django website)

Categories