开发者

How to Django models [closed]

开发者_运维百科Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 29 days ago.

Improve this question

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...


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜