代碼解讀基于DJango1.5.1
https://github.com/django/django.git
python django模型內(nèi)部類meta詳細(xì)解釋
如果你喜歡的話,可以把元類稱為“類工廠”(不要和工廠類搞混了:D) type就是Python的內(nèi)建元類,當(dāng)然了,你也可以創(chuàng)建自己的元類。
What is a metaclass in Python?
signal dispatching
from django.db.models.signals import m2m_changed
def toppings_changed(sender, **kwargs):
# Do something
pass
m2m_changed.connect(toppings_changed, sender=Pizza.toppings.through)
>>> p = Pizza.objects.create(...)
>>> t = Topping.objects.create(...)
>>> p.toppings.add(t)
the arguments sent to a m2m_changed handler (toppings_changed in the example above) would be:
Argument Value
sender Pizza.toppings.through (the intermediate m2m class)
instance p(the Pizza instance being modified)
action "pre_add"(followed by a separate signal with "post_add")
reverse False(Pizza contains the ManyToManyField(so this call modifies the forward relation)
model Topping(the class of the objects added to the Pizza)
pk_set {t.id}(since only Topping t was added to the relation)
using "default"(since the default router sends writes here)
Signal.connect(receiver, sender=None, weak=True, dispatch_uid=None)
receiver function 有時(shí)候也被稱為signal handler
Python中的類變量和成員變量
可以發(fā)現(xiàn):python的類變量和C++的靜態(tài)變量不同,并不是由類的所有對(duì)象共享。類本身?yè)碛凶约旱念愖兞浚ū4嬖趦?nèi)存),當(dāng)一個(gè)TestClass類的對(duì)象被構(gòu)造時(shí),會(huì)將當(dāng)前類變量拷貝一份給這個(gè)對(duì)象,當(dāng)前類變量的值是多少,這個(gè)對(duì)象拷貝得到的類變量的值就是多少;而且,通過對(duì)象來(lái)修改類變量,并不會(huì)影響其他對(duì)象的類變量的值,因?yàn)榇蠹叶加懈髯缘母北?,更不?huì)影響類本身所擁有的那個(gè)類變量的值;只有類自己才能改變類本身?yè)碛械念愖兞康闹?/p>
如何理解 Python 的 Descriptor?
深入解析Python中的descriptor描述器的作用及用法
簡(jiǎn)明Python魔法-1
簡(jiǎn)明Python魔法-2