Wednesday, August 4, 2010

Django : Auto-Generate model.py file from PostgreSQL / MySQL / SQLite database

inspectdb

django-admin.py inspectdb
Introspects the database tables in the database pointed-to by the NAME setting and outputs a Django model module (a models.py file) to standard output.
This feature is meant as a shortcut, not as definitive model generation. After you run it, you'll want to look over the generated models yourself to make customizations. In particular, you'll need to rearrange models' order, so that models that refer to other models are ordered properly.
Primary keys are automatically introspected for PostgreSQL, MySQL and SQLite, in which case Django puts in the primary_key=True where needed.
inspectdb works with PostgreSQL, MySQL and SQLite. Foreign-key detection only works in PostgreSQL and with certain types of MySQL tables.
Example :
Use command
#python manage.py inspectdb >>Models.py
this will automatically convert the django database into Models.py file .

1 comment:

  1. creating models.py without
    i.e. FOREIGN KEY (xxx) REFERENCES yyy(zzz) ON DELETE CASCADE
    :-/

    ReplyDelete