Re: [问题] Djangobook上的一段话 看不懂哩

楼主: billy0131 (Pluto)   2016-10-15 16:34:53
※ 引述《left (881 forever)》之铭言:
: 各位大大
: 在djangobook上有一段说明为啥要data model的地方看不太懂
: 有人可以帮忙解说一下吗?
: 这里的introspection是指?
: 感谢
: http://djangobook.com/django-models/
: Introspection requires overhead and is imperfect.
: In order to provide convenient data-access APIs, Django needs to know
: the database layout somehow, and there are two ways of accomplishing this.
: The first way would be to explicitly describe the data in Python,
: and the second way would be to introspect the database at runtime to
: determine the data models.This second way seems cleaner, because
: the metadata about your tables lives in only one place, but it introduces
: a few problems. First, introspecting a database at runtime obviously
: requires overhead. If the framework had to introspect the database
: each time it processed a request, or even only when the Web server
: was initialized, this would incur an unacceptable level of overhead.
: (While some believe that level of overhead is acceptable,
: Django’s developers aim to trim as much framework overhead as possible.)
: Second, some databases, notably older versions of MySQL,
: do not store sufficient metadata for accurate and complete introspection.
这段就是在说web framework要能够跟数据库拿资料的话,
需要知道数据库的table, schema等资讯
然后web framework有两种方法可以知道这种资讯:
1. 写在程式里定义好数据库要有的表跟字段
2. 在程式跑起来的时候去检视(Introspect)数据库既有的字段
然后django采取的是第一种做法
因为第二种做法会增加不必要的复杂度
django实际上的作法就是在models.py里定义好资料的结构
然后下migrate的指令就可以生成database里的table
资料结构有变动时也是先改models.py的内容
再靠makemigrations跟migrate的指令来完成
这样能确保程式码跟数据库结构的一致性,避免一些奇怪的问题产生
作者: kevinkung (Jaguar)   2016-10-15 19:37:00
推。sqlite3的实用性似乎没有比其他db来的方便?

Links booklink

Contact Us: admin [ a t ] ucptt.com