Django: pull from database instead of push to database - database

I've inherited a v 1.0 Django/Python intranet site.
Django normally pushes from the Django code into the database. For say, table definitions (DDL).
Db architects associated with the project have a new database design that they want me to hook up to the site.
That is, I need somehow to pull the DDL defintions from out of the db and into Django code. E.g. some way to generate a new class from a table.
I know a lot of python but much less Django. Any ideas on how this might be done?

Yes, Django can work with pre-existing DB's. Check this out.
Basically, the command you need to pull models from the DB is python manage.py inspectdb > models.py

Related

How to setup an online database for my cross-platform games?

How to setup database whose data can be obtained by my cross-platform games (Flash on Kongregate, Flash and JavaScript on my WordPress page, Android and iOS) and my WordPress page as well? The data to be saved are gameplay count (total and for each platform) and high scores.
I have no experience about this database thing.
And my games will be made in Haxe.
You will want to have a redundant Database, preferably mysql. You can access a mysql database from almost any language / script.
Before you start that Project heads in have a look at what you need. I personally learn the best, if i find myself some example code, take it appart, look at each function if i don't know it already and then i try to find new ways of doing it with the functions provided.
Sqlite Database helper is a great start to create unlimited Databases just to see, how that works, how it looks inside a database, what is a primary key etc. You could even import the whole database to mysql, if you're very happy with it.
Choose a database of your choice, it could be mysql or mongodb.
Choose a server language of your choice and build a HTTP API to access your db. If you are using Haxe to build the server, you may want to target neko or php if you choose mysql as your db, or nodejs for mongodb. Because Haxe has mysql support for neko & php, and you can download mongodb drivers from npm if you choose mongodb and nodejs.
Then, from your games, just use the Http class provide in the Haxe standard library to access your Http API to get the data from your db.

Auto-deploy Zend Framework 2 application + Database schema + Actual data

Background:
I am using GitHub to store a ZF2 application.
The database schema + the actual data stored inside the schema are not being stored inside a version control. At the moment I am in development mode, so I have some database dump scripts that I load into the database when I need to. I also tweak entries in the database via phpMyAdmin when I need ongoing granular control for immediate testing purposes. I am also looking into using Doctrire ORM, so my schema will be part of my code via Annotations, and that will be checked into GitHub. Doctrine ORM will generate the actual schema for me, although it is still a separate step in the deployment process. The actual data however, will still be outside of the application and outside of the repository and currently has to be dealt with separately and is not automated.
Goal:
I want to be able to deploy ZF2 application and the database schema, and the data onto Zend Server and have it "just work" in the most automated, least manual way possible.
Question:
What is a recommended, best practice way to deploy every aspect of ZF2 application in the most automated, least manual way possible and have it "just work"? Let's focus on the Development and Testing mode here, as in Production it may be good to have separate deployment steps to protect against accidental live data overwrites.
You can try Phing (http://www.phing.info/) for deploying your PHP application, adjusting directory permissions, running database migrations, running unit tests, etc. I used Phing in couple of my projects with great success.

Postgresql changes in Heroku and Django combo with shared database

I've just started using Heroku with Django and it seems great. However, when I change my existing models I'm not sure how to run those changes to the Heroku environment. The syncdb works just fine when adding all new database tables, but how should I modify existing tables?
I found out that Heroku provides psql access only to a dedicated database so that's out of the question. I haven't tried South but it seems like a solution.
So I guess I'm asking how to make database changes with Django and Heroku?
What you are asking for is called "schema migration" or even "schema evolution". Django has some documentation about it on the wiki.
Django's syncdb command does not support that. As a matter of fact, the documentation for syncdb is clear:
Creates the database tables for all apps in INSTALLED_APPS whose
tables have not already been created
Rather, django proposes to use drop the tables manually and then to run syncdb again in the documentation of the deprecated reset command:
You can also use ALTER TABLE or DROP TABLE statements manually.
But fear not, there are many reusable apps to help you with proper schema migrations and hopefully you can pick the one that suits you best. Rather than elaborate in my answer, please let me link an article I wrote about Django schema migration which compares all current solutions.
South works great on Heroku.

Prevent syncdb from updating database in Django?

I'd like to respect the database as a "read-only" and never write to it. Is there a way to easily prevent syncdb from even bothering to check to update the database?
With Django 1.2 and the ability to have multiple databases, it'd like to be able to query a database for information. I'd never need to actually write to that database.
However, I'd be scared if syncdb ran and attempted to update that database (because I may not have a technically read-only account to that database). Mainly, I'd just like to use/abuse the Django ORM as a way to query that database.
UPDATE: Sorry, I need to be able to sync one of the databases in settings.py, just not this specific one.
Heh, I guess I'll answer my own question (RTFM!)...
http://docs.djangoproject.com/en/dev/topics/db/multi-db/#an-example
def allow_syncdb(self, db, model):
...
That's a definite start...
If you don't need syncdb, don't run it, simple as that. Updating the database is what it does, so if you don't need that, you shouldn't run it - it doesn't do anything else.
However if you're actually asking how to prevent syncdb from running at all, one possibility would be to define a 'dummy' syncdb command inside one of your apps. Follow the custom management command instructions but just put pass inside the command's handle method. Django will always find your version of the command first, making it a no-op.
This issue came up for me when working with read-only mirrors of Microsof SQL Server databases (uhhg). Since you can't selectively run syncdb on a single app or database. But you have to run syncdb when you first create a new Django project or install an new app that requires it (like south). What I did was to put my read-only database in its own Django app and then add an empty South migration to that app. That way syncdb thinks south is handling db setup for those apps, and south doesn't do anything to them!
manage.py schemamigration ap_with_read_only_database --empty initial_empty_migration_that_does_nothing
That leaves you free to manage the schema of that db outside of django.

Is it possible to generate django models from the database?

I've been messing around with Django and the Django ORM at home, and I've got to say, I feel it is one of the best out there in terms of ease of use.
However, I was wondering if it was possible to use it in "reverse".
Basically what I would like to do is generate Django models from an existing database schema (from a project that doesn't use django and is pretty old).
Is this possible?
Update: the database in question is Oracle
Yes, use the inspectdb command:
http://docs.djangoproject.com/en/dev/ref/django-admin/#inspectdb
inspectdb
Introspects the database tables in the database pointed-to by the DATABASE_NAME setting and outputs a Django model module (a models.py file) to standard output.
Use this if you have a legacy database with which you'd like to use Django. The script will inspect the database and create a model for each table within it.
As you might expect, the created models will have an attribute for every field in the table. Note that inspectdb has a few special cases in its field-name output:
[...]
(Django 1.7.1) Simply running python manage.py inspectdb will create classes for all tables in database and display on console.
$ python manage.py inspectdb
Save this as a file by using standard Unix output redirection:
$ python manage.py inspectdb > models.py
(This works for me with mysql and django 1.9)
I have made a reusable app based on django's inspectdb command utility,
Django Inspectdb Refactor.
This breaks models into different files inside models folder from a existing database.
This helps managing models when they become large in number.
You can install it via pip:
pip install django-inspectdb-refactor
Then register the app in settings.py as inspectdb_refactor
After this you can use it from command line as :
python manage.py inspectdb_refactor --database=your_dbname_defined_in_settings --app=your_app_label
This will successfully create models folder with all the tables as different model files inside your app. For example:
More details can be found here.

Resources