ó
i4Vdc           @  sæ   d  Z  d d l m Z d d l m Z d d l m Z m Z m Z d d l	 m
 Z
 m Z d d l m Z d d l m Z d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z d e f d „  ƒ  YZ d „  Z d S(   uH
  
Accessors for related objects.

When a field defines a relation between two models, each model class provides
an attribute to access related instances of the other model class (unless the
reverse accessor has been disabled with related_name='+').

Accessors are implemented as descriptors in order to customize access and
assignment. This module defines the descriptor classes.

Forward accessors follow foreign keys. Reverse accessors trace them back. For
example, with the following models::

    class Parent(Model):
        pass

    class Child(Model):
        parent = ForeignKey(Parent, related_name='children')

 ``child.parent`` is a forward many-to-one relation. ``parent.children`` is a
reverse many-to-one relation.

There are three types of relations (many-to-one, one-to-one, and many-to-many)
and two directions (forward and reverse) for a total of six combinations.

1. Related instance on the forward side of a many-to-one or one-to-one
   relation: ``ForwardManyToOneDescriptor``.

   Uniqueness of foreign key values is irrelevant to accessing the related
   instance, making the many-to-one and one-to-one cases identical as far as
   the descriptor is concerned. The constraint is checked upstream (unicity
   validation in forms) or downstream (unique indexes in the database).

   If you're looking for ``ForwardOneToOneDescriptor``, use
   ``ForwardManyToOneDescriptor`` instead.

2. Related instance on the reverse side of a one-to-one relation:
   ``ReverseOneToOneDescriptor``.

   One-to-one relations are asymmetrical, despite the apparent symmetry of the
   name, because they're implemented in the database with a foreign key from
   one table to another. As a consequence ``ReverseOneToOneDescriptor`` is
   slightly different from ``ForwardManyToOneDescriptor``.

3. Related objects manager for related instances on the reverse side of a
   many-to-one relation: ``ReverseManyToOneDescriptor``.

   Unlike the previous two classes, this one provides access to a collection
   of objects. It returns a manager rather than an instance.

4. Related objects manager for related instances on the forward or reverse
   sides of a many-to-many relation: ``ManyToManyDescriptor``.

   Many-to-many relations are symmetrical. The syntax of Django models
   requires declaring them on one side but that's an implementation detail.
   They could be declared on the other side without any change in behavior.
   Therefore the forward and reverse descriptors can be the same.

   If you're looking for ``ForwardManyToManyDescriptor`` or
   ``ReverseManyToManyDescriptor``, use ``ManyToManyDescriptor`` instead.
iÿÿÿÿ(   t   unicode_literals(   t
   attrgetter(   t   connectionst   routert   transaction(   t   Qt   signals(   t   QuerySet(   t   cached_propertyt   ForwardManyToOneDescriptorc           B  sY   e  Z d  Z d „  Z e d „  ƒ Z d „  Z d „  Z d d „ Z	 d d „ Z
 d „  Z RS(	   u!  
    Accessor to the related object on the forward side of a many-to-one or
    one-to-one relation.

    In the example::

        class Child(Model):
            parent = ForeignKey(Parent, related_name='children')

    ``child.parent`` is a ``ForwardManyToOneDescriptor`` instance.
    c         C  s   | |  _  |  j  j ƒ  |  _ d  S(   N(   t   fieldt   get_cache_namet
   cache_name(   t   selft   field_with_rel(    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyt   __init__V   s    	c         C  s(   t  t d ƒ |  j j j j t f i  ƒ S(   Nu   RelatedObjectDoesNotExist(   t   typet   strR
   t   remote_fieldt   modelt   DoesNotExistt   AttributeError(   R   (    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyt   RelatedObjectDoesNotExistZ   s    	c         C  s   t  | |  j ƒ S(   N(   t   hasattrR   (   R   t   instance(    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyt	   is_cachede   s    c         K  sO   |  j  j j j } t | d t ƒ s9 |  j  j j j } n  | j d | ƒ j ƒ  S(   Nu   use_for_related_fieldst   hints(	   R
   R   R   t   _default_managert   getattrt   Falset   _base_managert
   db_managert   all(   R   R   t   manager(    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyt   get_queryseth   s    c   
        sX  | d  k r |  j ƒ  } n  | j d | d ƒ |  j j } |  j j ‰  ‡  f d †  | Dƒ } |  j j d } |  j j j ƒ  s— t	 |  j j ƒ d k rÄ i t
 ‡  f d †  | Dƒ ƒ d | j 6} n i | d |  j j ƒ  6} | j |   } |  j j j sB|  j j j ƒ  } x1 | D]& } | | | ƒ }	 t | | |	 ƒ qWn  | | ˆ  t |  j f S(   NR   i    c           s   i  |  ] } | ˆ  | ƒ “ q S(    (    (   t   .0t   inst(   t   instance_attr(    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pys
   <dictcomp>w   s   	 i   c         3  s   |  ] } ˆ  | ƒ d  Vq d S(   i    N(    (   R#   R$   (   R%   (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pys	   <genexpr>   s    u   %s__in(   t   NoneR"   t
   _add_hintsR
   t   get_foreign_related_valuet   get_local_related_valuet   foreign_related_fieldsR   t	   is_hiddent   lent   sett   namet   related_query_namet   filtert   multipleR   t   setattrt   TrueR   (
   R   t	   instancest   querysett   rel_obj_attrt   instances_dictt   related_fieldt   queryt   rel_obj_cache_namet   rel_objR   (    (   R%   sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyt   get_prefetch_querysetp   s"    *-c         C  s'  | d k r |  Sy t | |  j ƒ } Wn² t k
 rÚ |  j j | ƒ } d | k r] d } ng |  j d | ƒ } | j |  j j | ƒ ƒ } | j	 ƒ  } |  j j
 j sÄ t | |  j j
 j ƒ  | ƒ n  t | |  j | ƒ n X| d k r|  j j r|  j d |  j j j |  j j f ƒ ‚ n | Sd S(   uG  
        Get the related instance through the forward relation.

        With the example above, when getting ``child.parent``:

        - ``self`` is the descriptor managing the ``parent`` attribute
        - ``instance`` is the ``child`` instance
        - ``instance_type`` in the ``Child`` class (we don't need it)
        R   u   %s has no %s.N(   R&   R   R   R   R
   R)   R"   R0   t   get_reverse_related_filtert   getR   R1   R2   R   t   nullR   R   t   __name__R.   (   R   R   t   instance_typeR;   t   valt   qs(    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyt   __get__   s$    
	%c         C  sp  | d k rC |  j j t k rC t d | j j |  j j f ƒ ‚ n | d k	 r¢ t | |  j j	 j
 ƒ r¢ t d | | j j |  j j |  j j	 j
 j j f ƒ ‚ nÁ | d k	 rc| j j d k rá t j | j d | ƒ| j _ qc| j j d k rt j | j d | ƒ| j _ qc| j j d k	 rc| j j d k	 rct j | | ƒ s`t d | ƒ ‚ q`qcn  | d k rât | |  j d ƒ } | d k	 r¯t | |  j j	 j ƒ  d ƒ n  xl |  j j D] \ } } t | | j d ƒ q¼Wn< x9 |  j j D]+ \ } } t | | j t | | j ƒ ƒ qïWt | |  j | ƒ | d k	 rl|  j j	 j rlt | |  j j	 j ƒ  | ƒ n  d S(   uX  
        Set the related instance through the forward relation.

        With the example above, when setting ``child.parent = parent``:

        - ``self`` is the descriptor managing the ``parent`` attribute
        - ``instance`` is the ``child`` instance
        - ``value`` in the ``parent`` instance on the right of the equal sign
        u7   Cannot assign None: "%s.%s" does not allow null values.u4   Cannot assign "%r": "%s.%s" must be a "%s" instance.R   uG   Cannot assign "%r": the current database router prevents this relation.N(   R&   R
   R?   R   t
   ValueErrort   _metat   object_nameR.   t
   isinstanceR   R   t   _statet   dbR   t   db_for_writet	   __class__t   allow_relationR   R   R2   R   t   related_fieldst   attnameR1   (   R   R   t   valuet   relatedt   lh_fieldt   rh_field(    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyt   __set__¸   s<    %		!!$#N(   R@   t
   __module__t   __doc__R   R   R   R   R"   R&   R<   RD   RT   (    (    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyR	   I   s   			)t   ReverseOneToOneDescriptorc           B  sY   e  Z d  Z d „  Z e d „  ƒ Z d „  Z d „  Z d d „ Z	 d d „ Z
 d „  Z RS(	   u  
    Accessor to the related object on the reverse side of a one-to-one
    relation.

    In the example::

        class Restaurant(Model):
            place = OneToOneField(Place, related_name='restaurant')

    ``place.restaurant`` is a ``ReverseOneToOneDescriptor`` instance.
    c         C  s   | |  _  | j ƒ  |  _ d  S(   N(   RQ   R   R   (   R   RQ   (    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyR     s    	c         C  s%   t  t d ƒ |  j j j t f i  ƒ S(   Nu   RelatedObjectDoesNotExist(   R   R   RQ   t   related_modelR   R   (   R   (    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyR     s    	c         C  s   t  | |  j ƒ S(   N(   R   R   (   R   R   (    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyR     s    c         K  sI   |  j  j j } t | d t ƒ s3 |  j  j j } n  | j d | ƒ j ƒ  S(   Nu   use_for_related_fieldsR   (   RQ   RX   R   R   R   R   R   R    (   R   R   R!   (    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyR"     s    c   	        så   | d  k r |  j ƒ  } n  | j d | d ƒ t |  j j j ƒ } d „  ‰  ‡  f d †  | Dƒ } i | d |  j j j 6} | j |   } |  j j j	 ƒ  } x. | D]& } | | | ƒ } t
 | | | ƒ q¥ W| | ˆ  t |  j f S(   NR   i    c         S  s
   |  j  ƒ  S(   N(   t   _get_pk_val(   t   obj(    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyt   <lambda>-  t    c           s   i  |  ] } | ˆ  | ƒ “ q S(    (    (   R#   R$   (   R%   (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pys
   <dictcomp>.  s   	 u   %s__in(   R&   R"   R'   R   RQ   R
   RO   R.   R0   R   R2   R3   R   (	   R   R4   R5   R6   R7   R9   R:   R;   R   (    (   R%   sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyR<   '  s    	c         C  s   | d k r |  Sy t | |  j ƒ } Wn¸ t k
 rà | j ƒ  } | d k rW d } ns |  j j j | ƒ } y |  j d | ƒ j	 |   } Wn  |  j j
 j k
 r­ d } n Xt | |  j j j ƒ  | ƒ t | |  j | ƒ n X| d k r|  j d | j j |  j j ƒ  f ƒ ‚ n | Sd S(   u  
        Get the related instance through the reverse relation.

        With the example above, when getting ``place.restaurant``:

        - ``self`` is the descriptor managing the ``restaurant`` attribute
        - ``instance`` is the ``place`` instance
        - ``instance_type`` in the ``Place`` class (we don't need it)

        Keep in mind that ``Restaurant`` holds the foreign key to ``Place``.
        R   u   %s has no %s.N(   R&   R   R   R   RY   RQ   R
   t   get_forward_related_filterR"   R>   RX   R   R2   R   R   RL   R@   t   get_accessor_name(   R   R   RA   R;   t
   related_pkt   filter_args(    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyRD   :  s*    	
	c           s/  | d k r™ |  j j j rq y t ˆ  |  j ƒ } Wn t k
 rD q– Xt ˆ  |  j ƒ t | |  j j j	 d ƒ q+t
 d ˆ  j j |  j j ƒ  f ƒ ‚ n’t | |  j j ƒ sè t
 d | ˆ  j j |  j j ƒ  |  j j j j f ƒ ‚ nCˆ  j j d k rt j ˆ  j d | ƒˆ  j _ n | j j d k rNt j | j d ˆ  ƒ| j _ nL | j j d k	 ršˆ  j j d k	 ršt j | ˆ  ƒ sšt
 d | ƒ ‚ qšn  t ‡  f d †  |  j j j Dƒ ƒ } x: t |  j j j ƒ D]# \ } } t | | j | | ƒ qÕWt ˆ  |  j | ƒ t | |  j j j ƒ  ˆ  ƒ d S(   u¶  
        Set the related instance through the reverse relation.

        With the example above, when setting ``place.restaurant = restaurant``:

        - ``self`` is the descriptor managing the ``restaurant`` attribute
        - ``instance`` is the ``place`` instance
        - ``value`` in the ``restaurant`` instance on the right of the equal sign

        Keep in mind that ``Restaurant`` holds the foreign key to ``Place``.
        u7   Cannot assign None: "%s.%s" does not allow null values.u4   Cannot assign "%r": "%s.%s" must be a "%s" instance.R   uG   Cannot assign "%r": the current database router prevents this relation.c         3  s!   |  ] } t  ˆ  | j ƒ Vq d  S(   N(   R   RO   (   R#   R
   (   R   (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pys	   <genexpr>ž  s    N(   R&   RQ   R
   R?   R   R   R   t   delattrR2   R.   RE   RF   RG   R^   RH   RX   RI   RJ   R   RK   RL   RM   t   tupleR*   t	   enumeratet   local_related_fieldsRO   R   (   R   R   RP   R;   R_   t   indexR
   (    (   R   sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyRT   i  s>    		!!$%"N(   R@   RU   RV   R   R   R   R   R"   R&   R<   RD   RT   (    (    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyRW     s   	
		/t   ReverseManyToOneDescriptorc           B  s;   e  Z d  Z d „  Z e d „  ƒ Z d d „ Z d „  Z RS(   u¹  
    Accessor to the related objects manager on the reverse side of a
    many-to-one relation.

    In the example::

        class Child(Model):
            parent = ForeignKey(Parent, related_name='children')

    ``parent.children`` is a ``ReverseManyToOneDescriptor`` instance.

    Most of the implementation is delegated to a dynamically defined manager
    class built by ``create_forward_many_to_many_manager()`` defined below.
    c         C  s   | |  _  | j |  _ d  S(   N(   t   relR
   (   R   Rg   (    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyR   ¼  s    	c         C  s   t  |  j j j j |  j ƒ S(   N(   t"   create_reverse_many_to_one_managerRg   RX   R   RL   (   R   (    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyt   related_manager_clsÀ  s    c         C  s   | d k r |  S|  j | ƒ S(   uM  
        Get the related objects through the reverse relation.

        With the example above, when getting ``parent.children``:

        - ``self`` is the descriptor managing the ``children`` attribute
        - ``instance`` is the ``parent`` instance
        - ``instance_type`` in the ``Parent`` class (we don't need it)
        N(   R&   Ri   (   R   R   RA   (    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyRD   Ç  s    
c         C  s    |  j  | ƒ } | j | ƒ d S(   ua  
        Set the related objects through the reverse relation.

        With the example above, when setting ``parent.children = children``:

        - ``self`` is the descriptor managing the ``children`` attribute
        - ``instance`` is the ``parent`` instance
        - ``value`` in the ``children`` sequence on the right of the equal sign
        N(   RD   R-   (   R   R   RP   R!   (    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyRT   Ö  s    
N(	   R@   RU   RV   R   R   Ri   R&   RD   RT   (    (    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyRf   ¬  s
   	c           s#   d |  f ‡  ‡ f d †  ƒ  Y‰  ˆ  S(   uæ   
    Create a manager for the reverse side of a many-to-one relation.

    This manager subclasses another manager, generally the default manager of
    the related model, and adds behaviors specific to many-to-one relations.
    t   RelatedManagerc             s  e  Z ‡  ‡ f d  †  Z ‡ f d †  Z e Z ‡  f d †  Z d ‡  f d † Z d „  Z	 e e	 _
 ‡  f d †  Z e e _
 ‡  f d †  Z e e _
 ‡  f d †  Z e e _
 ˆ j j rí d „  Z e e _
 d	 „  Z e e _
 d
 „  Z e e _
 n  d „  Z e e _
 RS(   c           sN   t  ˆ  |  ƒ j ƒ  | |  _ ˆ j |  _ ˆ j |  _ i | |  j j 6|  _ d  S(   N(   t   superR   R   RX   R   R
   R.   t   core_filters(   R   R   (   Rj   Rg   (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyR   í  s
    	c           s:   t  |  j | j d ƒ ƒ } t | j ˆ  ƒ } | |  j ƒ S(   Nu   manager(   R   R   t   popRh   RL   R   (   R   t   kwargsR!   t   manager_class(   Rg   (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyt   __call__ö  s    c           s=  y |  j  j |  j j ƒ  SWnt t f k
 r8|  j pR t j |  j	 d |  j  ƒ} t
 | j j } t ˆ  |  ƒ j ƒ  } | j d |  j  ƒ |  j r« | j |  j ƒ } n  | j |  j   } xQ |  j j D]C } t |  j  | j ƒ } | d  k s| d k rÊ | rÊ | j ƒ  SqÊ Wi i |  j  |  j  j 6|  j 6| _ | SXd  S(   NR   u    (   R   t   _prefetched_objects_cacheR
   R/   R   t   KeyErrort   _dbR   t   db_for_readR   R   t   featurest!   interprets_empty_strings_as_nullsRk   R"   R'   t   usingR0   Rl   R*   R   RO   R&   t   nonet   pkt   _known_related_objects(   R   RJ   t   empty_strings_as_nullRC   R
   RB   (   Rj   (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyR"   þ  s     $	#c   	        s   | d  k r$ t ˆ |  ƒ j ƒ  } n  | j d | d ƒ | j | j pM |  j ƒ } |  j j } |  j j ‰  ‡  f d †  | Dƒ } i | d |  j j	 6} | j
 |   } x4 | D], } | | | ƒ } t | |  j j	 | ƒ q® W|  j j ƒ  } | | ˆ  t | f S(   NR   i    c           s   i  |  ] } | ˆ  | ƒ “ q S(    (    (   R#   R$   (   R%   (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pys
   <dictcomp>  s   	 u   %s__in(   R&   Rk   R"   R'   Rw   Rs   R
   R)   R(   R.   R0   R2   R/   R   (	   R   R4   R5   R6   R7   R9   R;   R   R   (   Rj   (   R%   sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyR<     s    c           s3  | j  d t ƒ } t | ƒ } t j ˆ  j d ˆ  j ƒ} ‡  f d †  } | rë g  } xY | D]Q } | | ƒ | j j s‰ | j j	 | k rœ t
 d | ƒ ‚ n  | j | j ƒ q[ Wˆ  j j j | ƒ j d | ƒ j i ˆ  j ˆ  j j 6  nD t j d | d t ƒ * x" | D] } | | ƒ | j ƒ  qWWd  QXd  S(   Nu   bulkR   c           sQ   t  |  ˆ  j ƒ s4 t d ˆ  j j j |  f ƒ ‚ n  t |  ˆ  j j ˆ  j ƒ d  S(   Nu   '%s' instance expected, got %r(	   RH   R   t	   TypeErrorRF   RG   R2   R
   R.   R   (   RZ   (   R   (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyt   check_and_update_obj*  s    uA   %r instance isn't saved. Use bulk=False or save the object first.t   pk__inRw   t	   savepoint(   Rm   R3   t   listR   RK   R   R   RI   t   addingRJ   RE   t   appendRy   R   Rw   R0   t   updateR
   R.   R   t   atomicR   t   save(   R   t   objsRn   t   bulkRJ   R}   t   pksRZ   (    (   R   sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyt   add%  s&    
$
c           sM   |  j  | |  j j <t j |  j d |  j  ƒ} t ˆ  |  j | ƒ ƒ j |   S(   NR   (	   R   R
   R.   R   RK   R   Rk   R   t   create(   R   Rn   RJ   (   Rj   (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyRŠ   E  s    c           sM   |  j  | |  j j <t j |  j d |  j  ƒ} t ˆ  |  j | ƒ ƒ j |   S(   NR   (	   R   R
   R.   R   RK   R   Rk   R   t   get_or_create(   R   Rn   RJ   (   Rj   (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyR‹   K  s    c           sM   |  j  | |  j j <t j |  j d |  j  ƒ} t ˆ  |  j | ƒ ƒ j |   S(   NR   (	   R   R
   R.   R   RK   R   Rk   R   t   update_or_create(   R   Rn   RJ   (   Rj   (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyRŒ   Q  s    c         _  s»   | s
 d  S| j  d t ƒ } |  j j |  j ƒ } t ƒ  } x^ | D]V } |  j j | ƒ | k rr | j | j ƒ qA |  j j	 j
 j d | |  j f ƒ ‚ qA W|  j |  j d | ƒ | ƒ d  S(   Nu   bulku   %r is not related to %r.R~   (   Rm   R3   R
   R(   R   R-   R)   R‰   Ry   R   R   R   t   _clearR0   (   R   R†   Rn   R‡   RB   t   old_idsRZ   (    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyt   removeY  s    	c         [  s&   | j  d t ƒ } |  j |  | ƒ d  S(   Nu   bulk(   Rm   R3   R   (   R   Rn   R‡   (    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyt   clearj  s    c      	   S  s°   t  j |  j d |  j ƒ} | j | ƒ } | rM | j i d  |  j j 6  n_ t	 j
 d | d t ƒ E x= | D]5 } t | |  j j d  ƒ | j d |  j j g ƒ qm WWd  QXd  S(   NR   Rw   R   t   update_fields(   R   RK   R   R   Rw   Rƒ   R&   R
   R.   R   R„   R   R2   R…   (   R   R5   R‡   RJ   RZ   (    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyR   o  s    c   	      [  s7  t  | ƒ } | j d t ƒ } | j d t ƒ } |  j j r t j |  j d |  j	 ƒ} t
 j d | d t ƒ ¬ | r– |  j ƒ  |  j d | | Œ n t |  j | ƒ j ƒ  ƒ } g  } x7 | D]/ } | | k rà | j | ƒ q¾ | j | ƒ q¾ W|  j d | | Œ |  j d | | Œ Wd  QXn |  j d | | Œ d  S(   Nu   bulku   clearR   Rw   R   R‡   (   Rb   Rm   R3   R   R
   R?   R   RK   R   R   R   R„   R   R‰   R-   Rw   R    R   R‚   (	   R   R†   Rn   R‡   R   RJ   t   old_objst   new_objsRZ   (    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyR-   |  s$    
N(   R@   RU   R   Rp   R3   t   do_not_call_in_templatesR"   R&   R<   R‰   t   alters_dataRŠ   R‹   RŒ   R
   R?   R   R   R   R-   (    (   Rj   Rg   (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyRj   ì  s,   												(    (   t
   superclassRg   (    (   Rj   Rg   sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyRh   ä  s    ­t   ManyToManyDescriptorc           B  s8   e  Z d  Z e d „ Z e d „  ƒ Z e d „  ƒ Z RS(   uá  
    Accessor to the related objects manager on the forward and reverse sides of
    a many-to-many relation.

    In the example::

        class Pizza(Model):
            toppings = ManyToManyField(Topping, related_name='pizzas')

    ``pizza.toppings`` and ``topping.pizzas`` are ``ManyToManyDescriptor``
    instances.

    Most of the implementation is delegated to a dynamically defined manager
    class built by ``create_forward_many_to_many_manager()`` defined below.
    c         C  s#   t  t |  ƒ j | ƒ | |  _ d  S(   N(   Rk   R—   R   t   reverse(   R   Rg   R˜   (    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyR   ­  s    c         C  s
   |  j  j S(   N(   Rg   t   through(   R   (    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyR™   ²  s    c         C  s@   |  j  r |  j j n	 |  j j } t | j j |  j d |  j  ƒS(   NR˜   (   R˜   Rg   RX   R   t#   create_forward_many_to_many_managerR   RL   (   R   R   (    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyRi   ¹  s
    !		(	   R@   RU   RV   R   R   t   propertyR™   R   Ri   (    (    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyR—   œ  s   c           s&   d |  f ‡  ‡ ‡ f d †  ƒ  Y‰  ˆ  S(   uç   
    Create a manager for the either side of a many-to-many relation.

    This manager subclasses another manager, generally the default manager of
    the related model, and adds behaviors specific to many-to-many relations.
    t   ManyRelatedManagerc             s"  e  Z d ‡  ‡ ‡ f d  † Z ‡ ‡ f d †  Z e Z d „  Z ‡  f d †  Z d ‡  f d † Z	 ‡ f d †  Z
 e e
 _ ‡ f d †  Z e e _ ‡  f d †  Z e e _ ‡ f d †  Z e e _ ‡  f d	 †  Z e e _ ‡  f d
 †  Z e e _ ‡  f d †  Z e e _ d „  Z ‡  f d †  Z RS(   c           sè  t  ˆ  |  ƒ j ƒ  | |  _ ˆ s‚ ˆ j |  _ ˆ j j ƒ  |  _ ˆ j j |  _ ˆ j j	 ƒ  |  _
 ˆ j j ƒ  |  _ ˆ j |  _ nZ ˆ j |  _ ˆ j j |  _ ˆ j j ƒ  |  _ ˆ j j ƒ  |  _
 ˆ j j	 ƒ  |  _ t |  _ ˆ j |  _ ˆ |  _ |  j j j |  j
 ƒ |  _ |  j j j |  j ƒ |  _ i  |  _ xI |  j j D]; \ } } d |  j | j f } t | | j ƒ |  j | <q=W|  j j | ƒ |  _ d  |  j k r¼t d | |  j
 f ƒ ‚ n  | j d  k rät d | j j  ƒ ‚ n  d  S(   Nu   %s__%su\   "%r" needs to have a value for field "%s" before this many-to-many relationship can be used.u]   %r instance needs to have a primary key value before a many-to-many relationship can be used.(!   Rk   R   R   R   R
   R/   t   query_field_nameR.   t   prefetch_cache_namet   m2m_field_namet   source_field_namet   m2m_reverse_field_namet   target_field_namet   symmetricalRX   R   R™   R˜   RF   t	   get_fieldt   source_fieldt   target_fieldRl   RN   R   RO   R(   t   related_valR&   RE   Ry   RL   R@   (   R   R   RR   RS   t   core_filter_key(   Rœ   Rg   R˜   (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyR   Ì  s<    				c           s@   t  |  j | j d ƒ ƒ } t | j ˆ  ˆ ƒ } | d |  j ƒ S(   Nu   managerR   (   R   R   Rm   Rš   RL   R   (   R   Rn   R!   Ro   (   Rg   R˜   (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyRp   ø  s    c         S  s¶   t  i |  j |  j 6  } t | t ƒ p2 | j ƒ  } | r\ | t  i | d |  j 6  M} n  |  j r² t  i |  j |  j 6  } | r¥ | t  i | d |  j 6  M} n  | | O} n  | S(   Nu   %s__in(   R   R§   R    RH   R   t   _has_filtersR¢   R£   (   R   t   removed_valst   filterst   removed_vals_filterst   symmetrical_filters(    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyt   _build_remove_filters   s    !	c           sŒ   y |  j  j |  j SWnp t t f k
 r‡ t ˆ  |  ƒ j ƒ  } | j d |  j  ƒ |  j rq | j	 |  j ƒ } n  | j
 ƒ  j |  j   SXd  S(   NR   (   R   Rq   Rž   R   Rr   Rk   R"   R'   Rs   Rw   t   _next_is_stickyR0   Rl   (   R   RC   (   Rœ   (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyR"     s    	c           s  | d  k r$ t ˆ |  ƒ j ƒ  } n  | j d | d ƒ | j | j pM |  j ƒ } i | d |  j 6} | j ƒ  j |   } |  j	 j
 j |  j ƒ ‰ |  j	 j
 j ‰ t | j ‰  ˆ  j j ‰ | j d ‡ ‡ f d †  ˆ j Dƒ ƒ } | ‡ f d †  ‡  ‡ f d †  t |  j f S(   NR   i    u   %s__int   selectc           s9   i  |  ]/ } d  ˆ ˆ  ƒ ˆ | j  ƒ f d | j “ q S(   u   %s.%su   _prefetch_related_val_%s(   t   columnRO   (   R#   t   f(   t
   join_tablet   qn(    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pys
   <dictcomp>0  s   	c           s   t  ‡  f d †  ˆ j Dƒ ƒ S(   Nc         3  s%   |  ] } t  ˆ  d  | j ƒ Vq d S(   u   _prefetch_related_val_%sN(   R   RO   (   R#   R²   (   t   result(    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pys	   <genexpr>5  s   (   Rb   Rd   (   Rµ   (   t   fk(   Rµ   sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyR[   4  s   c           s    t  ‡ ‡  f d †  ˆ j Dƒ ƒ S(   Nc         3  s-   |  ]# } | j  t ˆ | j ƒ ˆ  ƒ Vq d  S(   N(   t   get_db_prep_valueR   RO   (   R#   R²   (   t
   connectionR$   (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pys	   <genexpr>9  s   (   Rb   R*   (   R$   (   R¸   R¶   (   R$   sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyR[   8  s   (   R&   Rk   R"   R'   Rw   Rs   R   R¯   R0   R™   RF   R¤   R    t   db_tableR   RJ   t   opst
   quote_namet   extraRd   R   Rž   (   R   R4   R5   R9   (   Rœ   (   R¸   R¶   R³   R´   sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyR<     s$    		c           s¶   ˆ  j  j j s: |  j  j } t d | j | j f ƒ ‚ n  t j |  j  d |  j ƒ} t	 j
 d | d t ƒ C |  j |  j |  j | Œ |  j r¬ |  j |  j |  j | Œ n  Wd  QXd  S(   Nui   Cannot use add() on a ManyToManyField which specifies an intermediary model. Use %s.%s's Manager instead.R   Rw   R   (   R™   RF   t   auto_createdR   t	   app_labelRG   R   RK   R   R   R„   R   t
   _add_itemsR    R¢   R£   (   R   R†   t   optsRJ   (   Rg   (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyR‰   @  s    	c           sW   ˆ  j  j j s: |  j  j } t d | j | j f ƒ ‚ n  |  j |  j |  j | Œ d  S(   Nul   Cannot use remove() on a ManyToManyField which specifies an intermediary model. Use %s.%s's Manager instead.(	   R™   RF   R½   R   R¾   RG   t   _remove_itemsR    R¢   (   R   R†   RÀ   (   Rg   (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyR   R  s    c           s  t  j |  j d |  j ƒ} t j d | d t ƒ Ô t j j	 d |  j d d d |  j d |  j
 d |  j d	 d  d | ƒ |  j t ˆ  |  ƒ j ƒ  j | ƒ ƒ } |  j j j | ƒ j | ƒ j ƒ  t j j	 d |  j d d
 d |  j d |  j
 d |  j d	 d  d | ƒ Wd  QXd  S(   NR   Rw   R   t   sendert   actionu	   pre_clearR˜   R   t   pk_setu
   post_clear(   R   RK   R™   R   R   R„   R   R   t   m2m_changedt   sendR˜   R   R&   R®   Rk   R"   Rw   R   R0   t   delete(   R   RJ   R«   (   Rœ   (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyR   ]  s    '"c   
   	     sk  ˆ  j  j j s: |  j  j } t d | j | j f ƒ ‚ n  t | ƒ } | j d t ƒ } t	 j
 |  j  d |  j ƒ} t j d | d t ƒ Ú | r¬ |  j ƒ  |  j | Œ  nµ t |  j | ƒ j |  j j j d t ƒƒ } g  } xe | D]] } t | |  j ƒ r|  j j | ƒ d n | }	 |	 | k r6| j |	 ƒ qæ | j | ƒ qæ W|  j | Œ  |  j | Œ  Wd  QXd  S(   Nuj   Cannot set values on a ManyToManyField which specifies an intermediary model. Use %s.%s's Manager instead.u   clearR   Rw   R   t   flati    (   R™   RF   R½   R   R¾   RG   Rb   Rm   R   R   RK   R   R   R„   R   R‰   R-   Rw   t   values_listR¦   RO   R3   RH   R   R(   R   R‚   (
   R   R†   Rn   RÀ   R   RJ   RŽ   R“   RZ   t   fk_val(   Rg   (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyR-   l  s*    
-.c           sŠ   |  j  j j s: |  j  j } t d | j | j f ƒ ‚ n  t j |  j j	 d |  j ƒ} t
 ˆ  |  j | ƒ ƒ j |   } |  j | ƒ | S(   Nul   Cannot use create() on a ManyToManyField which specifies an intermediary model. Use %s.%s's Manager instead.R   (   R™   RF   R½   R   R¾   RG   R   RK   R   RL   Rk   R   RŠ   R‰   (   R   Rn   RÀ   RJ   t   new_obj(   Rœ   (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyRŠ   ‘  s    !c           se   t  j |  j j d |  j ƒ} t ˆ  |  j | ƒ ƒ j |   \ } } | r[ |  j | ƒ n  | | f S(   NR   (   R   RK   R   RL   Rk   R   R‹   R‰   (   R   Rn   RJ   RZ   t   created(   Rœ   (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyR‹   ¡  s
    'c           se   t  j |  j j d |  j ƒ} t ˆ  |  j | ƒ ƒ j |   \ } } | r[ |  j | ƒ n  | | f S(   NR   (   R   RK   R   RL   Rk   R   RŒ   R‰   (   R   Rn   RJ   RZ   RÌ   (   Rœ   (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyRŒ   «  s
    'c         W  s¿  d d l  m } | r»t ƒ  } xõ | D]í } t | |  j ƒ rÕ t j | |  j ƒ s~ t d | |  j j	 j
 | j	 j
 f ƒ ‚ n  |  j j j | ƒ j | ƒ d } | d  k rÅ t d | | f ƒ ‚ n  | j | ƒ q& t | | ƒ rt d |  j j j | f ƒ ‚ q& | j | ƒ q& Wt j |  j d |  j ƒ} |  j j j | ƒ j | d t ƒj i |  j d | 6| d	 | 6  }	 | t |	 ƒ } t j d
 | d t ƒ |  j s¹| |  j k rÿt j  j! d |  j d d d |  j d |  j d |  j d | d
 | ƒ n  |  j j j | ƒ j" g  | D]2 }
 |  j i |  j d d | 6|
 d | 6  ^ qƒ |  j sl| |  j k r²t j  j! d |  j d d d |  j d |  j d |  j d | d
 | ƒ n  Wd  QXn  d  S(   Niÿÿÿÿ(   t   ModeluH   Cannot add "%r": instance is on database "%s", value is on database "%s"i    u1   Cannot add "%r": the value for field "%s" is Noneu   '%s' instance expected, got %rR   RÈ   u   %s__inRw   R   RÂ   RÃ   u   pre_addR˜   R   RÄ   u   %s_idu   post_add(#   t   django.db.modelsRÍ   R-   RH   R   R   RM   R   RE   RI   RJ   R™   RF   R¤   R(   R&   R‰   R|   RG   RK   R   Rw   RÉ   R3   R0   R§   R   R„   R   R˜   R    R   RÅ   RÆ   t   bulk_create(   R   R    R¢   R†   RÍ   t   new_idsRZ   RÊ   RJ   t   valst   obj_id(    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyR¿   µ  sP    	%=c           s´  | s
 d  St  ƒ  } xS | D]K } t | |  j ƒ rX |  j j | ƒ d } | j | ƒ q | j | ƒ q Wt j |  j d |  j	 ƒ} t
 j d | d t ƒ t j j d |  j d d d |  j	 d |  j d	 |  j d
 | d | ƒ t ˆ  |  ƒ j ƒ  } | j ƒ  r0| j | ƒ j i | d |  j j j 6  }	 n | }	 |  j |	 ƒ }
 |  j j j | ƒ j |
 ƒ j ƒ  t j j d |  j d d d |  j	 d |  j d	 |  j d
 | d | ƒ Wd  QXd  S(   Ni    R   Rw   R   RÂ   RÃ   u
   pre_removeR˜   R   RÄ   u   %s__inu   post_remove(   R-   RH   R   R¦   R(   R‰   R   RK   R™   R   R   R„   R   R   RÅ   RÆ   R˜   Rk   R"   R©   Rw   R0   RO   R®   R   RÇ   (   R   R    R¢   R†   RŽ   RZ   RÊ   RJ   t   target_model_qst   old_valsR«   (   Rœ   (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyRÁ   ö  s.    	"N(   R@   RU   R&   R   Rp   R3   R”   R®   R"   R<   R‰   R•   R   R   R-   RŠ   R‹   RŒ   R¿   RÁ   (    (   Rœ   Rg   R˜   (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyRœ   Ë  s,   ,	
&				#					A(    (   R–   Rg   R˜   (    (   Rœ   Rg   R˜   sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyRš   Ã  s    "ÿ ON(   RV   t
   __future__R    t   operatorR   t	   django.dbR   R   R   RÎ   R   R   t   django.db.models.queryR   t   django.utils.functionalR   t   objectR	   RW   Rf   Rh   R—   Rš   (    (    (    sy   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.pyt   <module>=   s   ¸«8	¸'