一个 Django 的超简单的数据库应用

K*K posted @ Thu, 11 Dec 2008 18:05:38 +0800 in 未归类 with tags django mysql python , 3591 readers

 昨天 Launch & Learn 的时候,做了个简单的 Django 的数据库应用,以配合 Li Li Zhang 的 ORM Course。

其实用 Django 做数据库应用真的很简单,不过比较适合用于重头设计的 Database Schema,然后用 syncdb 来同步数据库。

目前 Django 对 ForeignKey 和 One to One, One to Many, Many to Many 支持得都不错,最大的缺陷还是缺乏 Join 的支持导致多表联查的性能不好,这也是我在做开发的过程中碰到的最大问题。

其实用 Django 重头做一个数据库开发非常简单,用下面几步就可以了。

FIRST:
Create new project and a new app with django-admin.py
$ django-admin.py startproject [project_name]
$ cd [project_name]
$ django-admin.py startapp [app_name]

SECOND:
Edit the settings.py file to adjust db settings and installed apps.

THIRD:
Edit the [app_name]/models.py to modeling the database schema

FOURTH:
Edit the [prject_name]/urls.py to adjust the path of URL.

FIFTH:
Edit the [app_name]/views.py to realize your function.

SIXTH:
To run the manage.py to start the test server in the project.
$ ./manage.py runserver

THE END:
Use your browser to navigate to your http://localhost:8000 and enjoy in it. :-)

这里提供了一个范例,也是昨天在 Launch and Learn 上演示过的:www.box.net/shared/6036abumgv 中的 lnl.tar.bz2 文件,里面提供的 Steps 其实就是上面那段,只是代码中也提供了注释以方便阅读。


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter