作者billy0131 (Pluto)
看板Python
标题Re: [问题] Djangobook上的一段话 看不懂哩
时间Sat Oct 15 16:34:53 2016
※ 引述《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的指令来完成
这样能确保程式码跟资料库结构的一致性,避免一些奇怪的问题产生
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 119.14.40.110
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1476520496.A.F37.html
1F:→ kevinkung: 推。sqlite3的实用性似乎没有比其他db来的方便? 10/15 19:37