views:

71

answers:

1

I am exploring Django with MySQL & have a few things that I wanted to discuss -

  1. How can I add an index (on some field)? Can I do it through the Django Model Layer?
  2. If I want to migrate some old data into these new DB tables/models, will I have to write some script myself or does Django provide schema to schema mapping tools?
  3. If I need to change the schema for the existing Django Models, then what's the easiest way to do this? I know this depends on my schema but is it as simple as create a new column & run python manage.py syncdb ?
  4. Lastly how do I profile MySQL db of all the queries I run from Django Models? I am looking for something like DEBUG=True and TEMPLATE_DEBUG=True kind of solution, where I'll get the query performance (runtime etc.) on the browser.

Thanks in advance...

+8  A: 

How can I add an index (on some field)? Can I do it through the Django Model Layer?

You can. Take a look at the Field.db_index option.

If I want to migrate some old data into these new DB tables/models, will I have to write some script myself or does Django provide schema to schema mapping tools?

Django does not have a built in tool to do data migration. For migrating existing data you will have to write a custom script. This may be possible using an external migration tool (such as South, see below) but I haven't tried it.

Lastly If I need to change the schema for the existing Django Models, then what's the easiest way to do this? I know this depends on my schema but is it as simple as create a new column & run python manage.py syncdb ?

Use a migration tool. South is one of the popular and effective ones out there. There are others too. Using a tool will make your life much more easier for many reasons. For e.g. syncdb will not add new columns, only new tables.

How do I profile MySQL db of all the queries I run from Django Models? I am looking for something like DEBUG=True and TEMPLATE_DEBUG=True kind of solution, where I'll get the query performance (runtime etc.) on the browser.

One of the ways to accomplish this is to use this classic Django snippet. Using the snippet you can append statistics about the queries fired to render a page to the bottom of the page if DEBUG is set.

Update

Another way to display the debug information is to use the django-debug-toolbar. Thanks @diegueus9.

Manoj Govindan
@Manoj Cool stuff! thanks...
MovieYoda
i recommend http://github.com/robhudson/django-debug-toolbar for profile sql queries.
diegueus9
MovieYoda