[python-chinese] Django model 的 manager 是如何工作的?
jeff jie
bbmyth在gmail.com
星期一 十二月 3 23:33:24 HKT 2007
û´í£¬µÄÈ·ÓëContribute_to_classÕâ¸öº¯ÊýÓйء£¶ø¸Ã·½·¨±»Ö´Ðеĵط½Ç¡Ç¡ºÃÊÇModelµÄMateClassµÄ__new__()·½·¨ÀïÃæ£º
´úÂëÕâÑùд£º
# Add Fields inherited from parents
for parent in new_class._meta.parents:
for field in parent._meta.fields:
# Only add parent fields if they aren't defined for this
class.
try:
new_class._meta.get_field(field.name)
except FieldDoesNotExist:
field.contribute_to_class(new_class, field.name)
ħ·¨ÔÚÕâÀïʵÏÖ¡£ÒªÈ«²¿¿´¶®ÕâЩ´úÂë¿ÉÄܵò¹Ò»ÏÂPythonÔªÀà±à³Ì·½ÃæµÄ¿ÎÁË¡£ £º£©
http://www-128.ibm.com/developerworks/cn/linux/l-pymeta/index.html
ÔÚ07-12-3£¬book4e <book4e在gmail.com> дµÀ£º
>
> Äã¸æËßÎÒµÄÊǽáÂÛ£¬ÎÒÏëÖªµÀΪʲôÓÐÕâÑùµÄ½áÂÛ¡£
>
> return QuerySet(*self.model*) ÀïÃæµÄ model£¬Ò²¾ÍÊÇ PersonProfile£¬ÎÒÏëÊÇÔÚ
> PersonManager ³õʼ»¯µÄʱºò¾Í±»É趨ÁË£º
>
> objects = PersonManager()
>
> ÄÇô PersonManager ÊÇÈçºÎ¸ÐÖª PersonProfile µÄ£¬ÊÇ·ñºÍÏÂÃæµÄ´úÂëÓйأ¿
>
> def __init__(self):
> super(Manager, self).__init__()
> # Increase the creation counter, and save our local copy.
> self.creation_counter = Manager.creation_counter
> Manager.creation_counter += 1
> self.model = None
>
> def contribute_to_class(self, model, name):
> # TODO: Use weakref because of possible memory leak / circular
> reference.
> self.model = model
> setattr(model, name, ManagerDescriptor(self))
> if not hasattr(model, '_default_manager') or self.creation_counter< model._default_manager.creation_counter:
> model._default_manager = self
>
> ËÆºõÓë contribute_to_class ºÜÓйØÏµ£¬µ«ÎÒûÓп´¶®Õâ¶Î´úÂëµÄÒâ˼£¬Ò²Ã»ÕÒµ½ËüÊǺÎʱ±»µ÷Óõġ£Äܲ»ÄܾßÌå½²½²£¿
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
--
site:http://www.fallever.com
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071203/cde4fe79/attachment.htm
关于邮件列表 python-chinese 的更多信息