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