We basically do not write SQL (for example, create an index, create tables, etc.) for our models and instead rely on Django to generate it for us.
It is perfectly normal to start by developing your application at the model level, as you can rely on Django to create the database SQL code you need.
However, Django provides various functions for replicating these database functions:
- triggers: Django code or MySQL triggers
- indexes: can be specified using
db_index=True
- unique constraints:
unique = True
or unique_togther = (('field1', field2'),)
for a complex unique constraint.
The advantage of using Django instead of writing sql is that you abstract from the specific database you are using. In other words, you can be on SQLite
one day and switch to PostgresQL
or MySQL
the next, and the change will be relatively painless.
Example:
When you run this:
python manage.py syncdb
Django automatically creates tables, indexes, triggers, etc., it must support the models you created. If you don't like creating django for your database, you can always use:
python manage.py sqlall
This will print out the SQL statements that Django will need to have for its models to work properly. There are other sql
commands you can use:
see https://docs.djangoproject.com/en/1.3/ref/django-admin/#sql-appname-appname
source share