Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Best practices for database design

I'm starting a project and i've decided to use Django.

My question is regarding to the creation of the database. I've read the tutorial and some books and those always start creating the models, and then synchronizing the DataBase. I've to say, that's a little weird for me. I've always started with the DB, defining the schema, and after that creating my DB Abstractions (models, entities, etc).

I've check some external-pluggable apps and those use that "model first" practice too.

I can see some advantages for the "model-first" approach, like portability, re-deployment, etc.

But i also see some disadvantages: how to create indexes, the kind of index, triggers, views, SPs, etc.

So, How do you start a real life project?

like image 827
santiagobasulto Avatar asked Sep 23 '11 15:09

santiagobasulto


People also ask

Is Django good for database?

Django is especially useful for database-driven websites because of its Model-View-Template (MVT), and there's hardly any application without a database. The MVT allows developers to change the visual part of an app without affecting the business part of the logic, and vice versa.

Which DB works best with Django?

Django officially supports the following databases: PostgreSQL. MariaDB. MySQL.

How does Django handle large data?

Use bulk query. Use bulk queries to efficiently query large data sets and reduce the number of database requests. Django ORM can perform several inserts or update operations in a single SQL query. If you're planning on inserting more than 5000 objects, specify batch_size.

Can Django use multiple databases?

Django's admin doesn't have any explicit support for multiple databases. If you want to provide an admin interface for a model on a database other than that specified by your router chain, you'll need to write custom ModelAdmin classes that will direct the admin to use a specific database for content.


2 Answers

Triggers, views, and stored procedures aren't really a part of the Django world. It can be made to use them, but it's painful and unnecessary. Django's developers take the view that business logic belongs in Python, not in your database.

As for indexes, you can create them along with your models (with things like db_index and unique_together, or you can add them later via database migrations using something like South.

like image 67
Daniel Roseman Avatar answered Oct 19 '22 15:10

Daniel Roseman


Mostly we don't write SQL (e.g. create index, create tables, etc...) for our models, and instead rely on Django to generate it for us.

It is absolutely okay to start with designing your app from the model layer, as you can rely on Django to generate the correct database sql needed for you.

However, Django does provide various functions for you to replicate these database functions:

  • triggers: Django code or MySQL triggers
  • indexes: can be specified with db_index=True
  • unique constraints: unique = True or unique_togther = (('field1', field2'),) for composite unique constraint.

The advantage with using Django instead of writing sql is that you abstract yourself away from the particular database you are using. In other words, you could be on SQLite one day and switch to PostgresQL or MySQL the next and the change would be relatively painless.

Example:

When you run this:

python manage.py syncdb 

Django automatically creates the tables, indexes, triggers, etc, it needs to support the models you've created. If you are not comfortable with django creating your database for you, you can always use:

python manage.py sqlall 

This will print out the SQL statements that Django would need to have in order for its models to function properly. There are other sql commands for you to use:

see: https://docs.djangoproject.com/en/1.3/ref/django-admin/#sql-appname-appname

like image 7
Derek Kwok Avatar answered Oct 19 '22 15:10

Derek Kwok