Python论坛  - 讨论区

标题:[python-chinese] Django model 的 manager 是如何工作的?

2007年12月02日 星期日 17:59

book4e book4e在gmail.com
星期日 十二月 2 17:59:52 HKT 2007

ÎÒÃÇÖªµÀ Django ÖпÉÒÔ×Ô¶¨Òå Manager Ìṩ¶îÍâµÄ¹¦ÄÜ£¬±ÈÈ磺

    class PersonManager(models.Manager):
        ...
        def get_profile(self, name):
            profile = *self.get(name=name)
        ...
*
    class PersonProfile(models.Model):
        ...
        objects = PersonManager()
        ...

Õâʱºò£¬¿ÉÒÔ PersonProfile.objects.get_profile('abc') À´µÃµ½½á¹û¡£µ«ÕâÀïÎÒʼÖÕûÓÐÃ÷°×
PersionProfile ÊÇÈçºÎ¸½¼Óµ½ PersionManager Éϵģ¬self.get() Ôõô¾ÍÖªµÀÒªÔÚ PersonProfile
ÉϲéÕÒ£¬¶ø²»ÊDZðµÄ£¿

²é¿´ÁËÔ´´úÂ룬get() ·½·¨ÊÇÕâÑùµÄ£º

    def get(self, *args, **kwargs):
        return self.get_query_set().get(*args, **kwargs)

query_set ÕâÑùµÃµ½£º

    def get_query_set(self):
        """Returns a new QuerySet object.  Subclasses can override this
method
        to easily customize the behavior of the Manager.
        """
        return QuerySet(*self.model*)

µ½ÕâÀï¾Í¿´²»¶®ÁË£¬ÆÚÍû´ó¼Ò¸ø½²½²¡£
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071202/5a5dd859/attachment-0001.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年12月03日 星期一 01:36

jeff jie bbmyth在gmail.com
星期一 十二月 3 01:36:13 HKT 2007

self.get() Ôõô¾ÍÖªµÀÒªÔÚ PersonProfile ÉϲéÕÒ

ÿһ¸öModel¶¼»áÓÐ×Ô¼º¶ÔÓ¦µÄManager£¬Manager¿Ï¶¨ÖªµÀ×Ô¼ºÒªÕÒʲô¡£Ä㶼¿´µ½´úÂëÁË£¬QuerySet(self.model
)¡£ÕâÀïºÜÃ÷È·£¬Ã÷°×Ä㻹ҪÎÊɶÁËÄ󡣡£

ÔÚ07-12-2£¬book4e <book4e在gmail.com> дµÀ£º
>
> ÎÒÃÇÖªµÀ Django ÖпÉÒÔ×Ô¶¨Òå Manager Ìṩ¶îÍâµÄ¹¦ÄÜ£¬±ÈÈ磺
>
>     class PersonManager(models.Manager ):
>         ...
>         def get_profile(self, name):
>             profile = *self.get(name=name)
>         ...
> *
>     class PersonProfile(models.Model):
>         ...
>         objects = PersonManager ()
>         ...
>
> Õâʱºò£¬¿ÉÒÔ PersonProfile.objects.get_profile('abc') À´µÃµ½½á¹û¡£µ«ÕâÀïÎÒʼÖÕûÓÐÃ÷°×
> PersionProfile ÊÇÈçºÎ¸½¼Óµ½ PersionManager Éϵģ¬self.get() Ôõô¾ÍÖªµÀÒªÔÚ PersonProfile
> ÉϲéÕÒ£¬¶ø²»ÊDZðµÄ£¿
>
> ²é¿´ÁËÔ´´úÂ룬get() ·½·¨ÊÇÕâÑùµÄ£º
>
>     def get(self, *args, **kwargs):
>         return self.get_query_set().get(*args, **kwargs)
>
> query_set ÕâÑùµÃµ½£º
>
>     def get_query_set(self):
>         """Returns a new QuerySet object.  Subclasses can override this
> method
>         to easily customize the behavior of the Manager.
>         """
>         return QuerySet(*self.model*)
>
> µ½ÕâÀï¾Í¿´²»¶®ÁË£¬ÆÚÍû´ó¼Ò¸ø½²½²¡£
>
> _______________________________________________
> 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/4103e48f/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年12月03日 星期一 22:17

book4e book4e在gmail.com
星期一 十二月 3 22:17:08 HKT 2007

Äã¸æËßÎÒµÄÊǽáÂÛ£¬ÎÒÏëÖªµÀΪʲôÓÐÕâÑùµÄ½áÂÛ¡£

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 ºÜÓйØϵ£¬µ«ÎÒûÓп´¶®Õâ¶Î´úÂëµÄÒâ˼£¬Ò²Ã»ÕÒµ½ËüÊǺÎʱ±»µ÷Óõġ£Äܲ»ÄܾßÌå½²½²£¿
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071203/d3270344/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年12月03日 星期一 23:33

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 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年12月04日 星期二 08:41

book4e book4e在gmail.com
星期二 十二月 4 08:41:00 HKT 2007

ллÄãµÄÖ¸µã£¬ÎÒÕâ¾Í²¹¿ÎÈ¥¡£
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071204/31d3e98a/attachment-0001.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2024

    京ICP备05028076号