ó
®â0_c           @  s  d  Z  d d l m Z d d l 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 d	 l m Z d
 e f d „  ƒ  YZ 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(   uª
  
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 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).

2. Related instance on the forward side of a one-to-one
   relation: ``ForwardOneToOneDescriptor``.

   It avoids querying the database when accessing the parent link field in
   a multi-table inheritance scenario.

3. 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``.

4. 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.

5. 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_literalsN(   t
   attrgetter(   t   connectionst   routert   transaction(   t   Qt   signals(   t   QuerySet(   t   RemovedInDjango20Warning(   t   cached_propertyt   ForwardManyToOneDescriptorc           B  sb   e  Z d  Z d „  Z e d „  ƒ Z d „  Z d „  Z d	 d „ Z	 d „  Z
 d	 d „ Z d „  Z RS(
   uJ  
    Accessor to the related object on the forward side of a many-to-one or
    one-to-one (via ForwardOneToOneDescriptor subclass) 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(    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyt   __init__[   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   (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyt   RelatedObjectDoesNotExist_   s    	c         C  s   t  | |  j ƒ S(   N(   t   hasattrR   (   R   t   instance(    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyt	   is_cachedj   s    c         K  s‰   |  j  j j } t | j d t ƒ rj t | j d t ƒ s^ t j d j | j	 j
 ƒ t d ƒ n  | j } n	 | j } | j d | ƒ j ƒ  S(   Nu   use_for_related_fieldsu*   silence_use_for_related_fields_deprecationuQ   use_for_related_fields is deprecated, instead set Meta.base_manager_name on '{}'.i   t   hints(   R   R   R   t   getattrt   _default_managert   Falset   warningst   warnt   formatt   _metat   labelR   t   _base_managert
   db_managert   all(   R   R   t   related_modelt   manager(    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyt   get_querysetm   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(    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pys
   <dictcomp>„   s   	 i   c         3  s   |  ] } ˆ  | ƒ d  Vq d S(   i    N(    (   R*   R+   (   R,   (    sM   /tmp/pip-unpacked-wheel-BAJOf3/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,   sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyt   get_prefetch_queryset}   s"    *-c         C  s+   |  j  d | ƒ } | j |  j j | ƒ ƒ S(   NR   (   R)   t   getR   t   get_reverse_related_filter(   R   R   t   qs(    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyt
   get_objectœ   s    c         C  sý   | d k r |  Sy t | |  j ƒ } Wnˆ t k
 r° |  j j | ƒ } d | k r] d } n= |  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(   u=  
        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
        - ``cls`` is the ``Child`` class (we don't need it)
        u   %s has no %s.N(   R-   R   R   R   R   R0   RG   R   R8   R9   R   t   nullR   R   t   __name__R5   (   R   R   t   clsRB   t   val(    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyt   __get__¡   s     
	%c         C  s3  | d k	 re t | |  j j j j j ƒ re t d | | j j |  j j	 |  j j j j j f ƒ ‚ nÁ | d k	 r&| j
 j d k r¤ t j | j d | ƒ| j
 _ q&| j
 j d k r× t j | j d | ƒ| j
 _ q&| j
 j d k	 r&| j
 j d k	 r&t j | | ƒ s#t d | ƒ ‚ q#q&n  | d k r¥t | |  j d ƒ } | d k	 rr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	 r/|  j j j r/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`` is the ``parent`` instance on the right of the equal sign
        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-   t
   isinstanceR   R   R   R"   t   concrete_modelt
   ValueErrort   object_nameR5   t   _statet   dbR   t   db_for_writet	   __class__t   allow_relationR   R   R9   R   t   related_fieldst   attnameR8   (   R   R   t   valuet   relatedt   lh_fieldt   rh_field(    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyt   __set__Ç   s4    +		!!$#N(   RI   t
   __module__t   __doc__R   R	   R   R   R)   R-   RC   RG   RL   R\   (    (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyR
   N   s   				&t   ForwardOneToOneDescriptorc           B  s   e  Z d  Z d „  Z RS(   u  
    Accessor to the related object on the forward side of a one-to-one relation.

    In the example::

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

    ``restaurant.place`` is a ``ForwardOneToOneDescriptor`` instance.
    c           sË   |  j  j j rµ ˆ j ƒ  } |  j  j j } g  | j j D] } | j ^ q7 ‰  t ‡  f d †  | Dƒ ƒ sµ ‡ f d †  ˆ  Dƒ } | |   } ˆ j	 j
 | j	 _
 ˆ j	 j | j	 _ | Sn  t t |  ƒ j ˆ ƒ S(   Nc         3  s   |  ] } | ˆ  k Vq d  S(   N(    (   R*   R   (   t   fields(    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pys	   <genexpr>!  s    c           s"   i  |  ] } t  ˆ  | ƒ | “ q S(    (   R   (   R*   R   (   R   (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pys
   <dictcomp>"  s   	 (   R   R   t   parent_linkt   get_deferred_fieldsR   R"   t   concrete_fieldsRW   t   anyRQ   t   addingRR   t   superR_   RG   (   R   R   t   deferredt	   rel_modelR   t   kwargst   obj(    (   R`   R   sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyRG     s    "(   RI   R]   R^   RG   (    (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyR_   
  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(   RY   R   R   (   R   RY   (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyR   7  s    	c         C  s%   t  t d ƒ |  j j j t f i  ƒ S(   Nu   RelatedObjectDoesNotExist(   R   R   RY   R'   R   R   (   R   (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyR   ;  s    	c         C  s   t  | |  j ƒ S(   N(   R   R   (   R   R   (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyR   E  s    c         K  s†   |  j  j } t | j d t ƒ rg t | j d t ƒ s[ t j d j | j j	 ƒ t
 d ƒ n  | j } n	 | j } | j d | ƒ j ƒ  S(   Nu   use_for_related_fieldsu*   silence_use_for_related_fields_deprecationuQ   use_for_related_fields is deprecated, instead set Meta.base_manager_name on '{}'.i   R   (   RY   R'   R   R   R   R   R    R!   R"   R#   R   R$   R%   R&   (   R   R   R'   R(   (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyR)   H  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(   Rj   (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyR,   _  s    c           s   i  |  ] } | ˆ  | ƒ “ q S(    (    (   R*   R+   (   R,   (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pys
   <dictcomp>b  s   	 u   %s__in(   R-   R)   R.   R   RY   R   RW   R5   R7   R   R9   R:   R   (	   R   R;   R<   R=   R>   R@   RA   RB   R   (    (   R,   sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyRC   X  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
        - ``cls`` is the ``Place`` class (unused)

        Keep in mind that ``Restaurant`` holds the foreign key to ``Place``.
        R   u   %s has no %s.N(   R-   R   R   R   Rl   RY   R   t   get_forward_related_filterR)   RD   R'   R   R9   R   R   RT   RI   t   get_accessor_name(   R   R   RJ   RB   t
   related_pkt   filter_args(    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyRL   n  s*    	
	c           sø  | d k rb y t ˆ  |  j ƒ } Wn t k
 r5 qôXt ˆ  |  j ƒ t | |  j j j d ƒ 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 rt j | j d ˆ  ƒ| j _ nL | j j d k	 rcˆ  j j d k	 rct j | ˆ  ƒ sct d | ƒ ‚ qc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`` is the ``restaurant`` instance on the right of the equal sign

        Keep in mind that ``Restaurant`` holds the foreign key to ``Place``.
        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   RW   (   R*   R   (   R   (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pys	   <genexpr>É  s    N(   R-   R   R   R   t   delattrR9   RY   R   R5   RM   R'   RO   R"   RP   Rn   RQ   RR   R   RS   RT   RU   t   tupleR1   t	   enumeratet   local_related_fieldsRW   R   (   R   R   RX   RB   Ro   t   indexR   (    (   R   sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyR\     s4    	!!$%"N(   RI   R]   R^   R   R	   R   R   R)   R-   RC   RL   R\   (    (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyRk   *  s   	
		/t   ReverseManyToOneDescriptorc           B  sD   e  Z d  Z d „  Z e d „  ƒ Z d d „ Z 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   Rw   (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyR   ç  s    	c         C  s"   |  j  j } t | j j |  j  ƒ S(   N(   Rw   R'   t"   create_reverse_many_to_one_managerR   RT   (   R   R'   (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyt   related_manager_clsë  s    	c         C  s   | d k r |  S|  j | ƒ S(   u9  
        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
        - ``cls`` is the ``Parent`` class (unused)
        N(   R-   Ry   (   R   R   RJ   (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyRL   ô  s    
c         C  s   d |  j  j ƒ  f S(   Nu   reverse side of a related set(   Rw   Rn   (   R   (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyt   _get_set_deprecation_msg_params  s    c         C  s@   t  j d |  j ƒ  t d d ƒ|  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`` is the ``children`` sequence on the right of the equal sign
        uh   Direct assignment to the %s is deprecated due to the implicit save() that happens. Use %s.set() instead.t
   stackleveli   N(   R   R    Rz   R   RL   R4   (   R   R   RX   R(   (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyR\   	  s    

N(
   RI   R]   R^   R   R	   Ry   R-   RL   Rz   R\   (    (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyRv   ×  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 d „  Z d „  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(   Rf   R   R   R'   R   R   R5   t   core_filters(   R   R   (   R|   Rw   (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyR   %  s
    	c           s:   t  |  j | j d ƒ ƒ } t | j ˆ  ƒ } | |  j ƒ S(   Nu   manager(   R   R   t   popRx   RT   R   (   R   Ri   R(   t   manager_class(   Rw   (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyt   __call__.  s    c         S  sò   |  j  p! t j |  j d |  j ƒ} t | j j } | j d |  j ƒ |  j  re | 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| _ | S(   uX   
            Filter the queryset for the instance this manager is bound to.
            R   u    N(   t   _dbR   t   db_for_readR   R   R   t   featurest!   interprets_empty_strings_as_nullsR.   t   usingR7   R}   R   R1   R   RW   R-   t   nonet   pkt   _known_related_objects(   R   R<   RR   t   empty_strings_as_nullR   RK   (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyt   _apply_rel_filters6  s    $	#c         S  s>   y  |  j  j j |  j j ƒ  ƒ Wn t t f k
 r9 n Xd  S(   N(   R   t   _prefetched_objects_cacheR~   R   R6   R   t   KeyError(   R   (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyt   _remove_prefetched_objectsG  s     c           sX   y |  j  j |  j j ƒ  SWn6 t t f k
 rS t ˆ  |  ƒ j ƒ  } |  j | ƒ SXd  S(   N(	   R   R‹   R   R6   R   RŒ   Rf   R)   RŠ   (   R   R<   (   R|   (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyR)   M  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,   (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pys
   <dictcomp>]  s   	 u   %s__in(   R-   Rf   R)   R.   R…   R   R   R0   R/   R5   R7   R9   R6   R   (	   R   R;   R<   R=   R>   R@   RB   R   R   (   R|   (   R,   sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyRC   T  s    c           s=  ˆ  j  ƒ  | 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 ƒ qe 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(	   RM   R   t	   TypeErrorR"   RP   R9   R   R5   R   (   Rj   (   R   (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyt   check_and_update_objo  s    uA   %r instance isn't saved. Use bulk=False or save the object first.t   pk__inR…   t	   savepoint(   R   R~   R:   t   listR   RS   R   R   RQ   Re   RR   RO   t   appendR‡   R$   R…   R7   t   updateR   R5   R   t   atomicR   t   save(   R   t   objsRi   t   bulkRR   R   t   pksRj   (    (   R   sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyt   addi  s(    

$
c           sM   |  j  | |  j j <t j |  j d |  j  ƒ} t ˆ  |  j | ƒ ƒ j |   S(   NR   (	   R   R   R5   R   RS   R   Rf   R%   t   create(   R   Ri   RR   (   R|   (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyR›   Š  s    c           sM   |  j  | |  j j <t j |  j d |  j  ƒ} t ˆ  |  j | ƒ ƒ j |   S(   NR   (	   R   R   R5   R   RS   R   Rf   R%   t   get_or_create(   R   Ri   RR   (   R|   (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyRœ     s    c           sM   |  j  | |  j j <t j |  j d |  j  ƒ} t ˆ  |  j | ƒ ƒ j |   S(   NR   (	   R   R   R5   R   RS   R   Rf   R%   t   update_or_create(   R   Ri   RR   (   R|   (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyR   –  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   (   R~   R:   R   R/   R   R4   R0   Rš   R‡   R   R   R   t   _clearR7   (   R   R—   Ri   R˜   RK   t   old_idsRj   (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyt   removež  s    	c         [  s&   | j  d t ƒ } |  j |  | ƒ d  S(   Nu   bulk(   R~   R:   Rž   (   R   Ri   R˜   (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyt   clear¯  s    c      	   S  sº   |  j  ƒ  t j |  j d |  j ƒ} | j | ƒ } | rW | 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 ƒ qw WWd  QXd  S(   NR   R…   R‘   t   update_fields(   R   R   RS   R   R   R…   R”   R-   R   R5   R   R•   R   R9   R–   (   R   R<   R˜   RR   Rj   (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyRž   ´  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   R…   R‘   R˜   (   Rr   R~   R:   R   R   RH   R   RS   R   R   R   R•   R¡   Rš   R4   R…   R&   R    R“   (	   R   R—   Ri   R˜   R¡   RR   t   old_objst   new_objsRj   (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyR4   Â  s$    
N(   RI   R]   R   R€   R:   t   do_not_call_in_templatesRŠ   R   R)   R-   RC   Rš   t   alters_dataR›   Rœ   R   R   RH   R    R¡   Rž   R4   (    (   R|   Rw   (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyR|   $  s0   														(    (   t
   superclassRw   (    (   R|   Rw   sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyRx     s    »t   ManyToManyDescriptorc           B  sA   e  Z d  Z e d „ Z e d „  ƒ Z e d „  ƒ Z 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(   Rf   R¨   R   t   reverse(   R   Rw   R©   (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyR   ó  s    c         C  s
   |  j  j S(   N(   Rw   t   through(   R   (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/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©   Rw   R'   R   t#   create_forward_many_to_many_managerR   RT   (   R   R'   (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyRy   ÿ  s
    !		c         C  s;   d |  j  r d n d |  j  r. |  j j ƒ  n	 |  j j f S(   Nu   %s side of a many-to-many setu   reverseu   forward(   R©   Rw   Rn   R   R5   (   R   (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyRz   	  s    (
   RI   R]   R^   R   R   t   propertyRª   R	   Ry   Rz   (    (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/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             s4  e  Z d ‡  ‡ ‡ f d  † Z ‡ ‡ f d †  Z e Z d „  Z d „  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  |  _ i  |  _ x\ |  j j D]N \ } } d |  j | j f } t | | j ƒ |  j | <| j |  j | j <qFW|  j j | ƒ |  _ d  |  j k rßt d | |  j |  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.("   Rf   R   R   R   R   R6   t   query_field_nameR5   t   prefetch_cache_namet   m2m_field_namet   source_field_namet   m2m_reverse_field_namet   target_field_namet   symmetricalR'   R   Rª   R©   R"   t	   get_fieldt   source_fieldt   target_fieldR}   t   pk_field_namesRV   R   RW   R/   t   related_valR-   RO   R‡   RT   RI   (   R   R   RZ   R[   t   core_filter_key(   R­   Rw   R©   (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyR     s@    					c           s@   t  |  j | j d ƒ ƒ } t | j ˆ  ˆ ƒ } | d |  j ƒ S(   Nu   managerR   (   R   R   R~   R«   RT   R   (   R   Ri   R(   R   (   Rw   R©   (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyR€   G  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±   RM   R   t   _has_filtersR³   R´   (   R   t   removed_valst   filterst   removed_vals_filterst   symmetrical_filters(    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyt   _build_remove_filtersO  s    !	c         S  sG   | j  d |  j ƒ |  j r1 | j |  j ƒ } n  | j ƒ  j |  j   S(   uX   
            Filter the queryset for the instance this manager is bound to.
            R   (   R.   R   R   R…   t   _next_is_stickyR7   R}   (   R   R<   (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyRŠ   _  s    	c         S  s8   y |  j  j j |  j ƒ Wn t t f k
 r3 n Xd  S(   N(   R   R‹   R~   R¯   R   RŒ   (   R   (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyR   h  s    c           sR   y |  j  j |  j SWn6 t t f k
 rM t ˆ  |  ƒ j ƒ  } |  j | ƒ SXd  S(   N(   R   R‹   R¯   R   RŒ   Rf   R)   RŠ   (   R   R<   (   R­   (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyR)   n  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   columnRW   (   R*   t   f(   t
   join_tablet   qn(    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pys
   <dictcomp>‹  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   RW   (   R*   RÄ   (   t   result(    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pys	   <genexpr>  s   (   Rr   Rt   (   RÇ   (   t   fk(   RÇ   sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyt   <lambda>  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   RW   (   R*   RÄ   (   t
   connectionR+   (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pys	   <genexpr>”  s   (   Rr   R1   (   R+   (   RË   RÈ   (   R+   sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyRÉ   “  s   (   R-   Rf   R)   R.   R…   R   R®   RÁ   R7   Rª   R"   Rµ   R±   R   t   db_tableR   RR   t   opst
   quote_namet   extraRt   R   R¯   (   R   R;   R<   R@   (   R­   (   RË   RÈ   RÅ   RÆ   sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyRC   u  s$    		c           sÀ   ˆ  j  j j s: |  j  j } t d | j | j f ƒ ‚ n  |  j ƒ  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   R…   R‘   (   Rª   R"   t   auto_createdR   t	   app_labelRP   R   R   RS   R   R   R•   R   t
   _add_itemsR±   R³   R´   (   R   R—   t   optsRR   (   Rw   (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyRš   ›  s    
	c           sa   ˆ  j  j j s: |  j  j } t d | j | j f ƒ ‚ n  |  j ƒ  |  j |  j |  j	 | Œ d  S(   Nul   Cannot use remove() on a ManyToManyField which specifies an intermediary model. Use %s.%s's Manager instead.(
   Rª   R"   RÐ   R   RÑ   RP   R   t   _remove_itemsR±   R³   (   R   R—   RÓ   (   Rw   (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyR    ­  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 ƒ  |  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   R…   R‘   t   sendert   actionu	   pre_clearR©   R   t   pk_setu
   post_clear(   R   RS   Rª   R   R   R•   R   R   t   m2m_changedt   sendR©   R   R-   R   RÀ   Rf   R)   R…   R   R7   t   delete(   R   RR   R½   (   R­   (    sM   /tmp/pip-unpacked-wheel-BAJOf3/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   R…   R‘   t   flati    (   Rª   R"   RÐ   R   RÑ   RP   Rr   R~   R   R   RS   R   R   R•   R¡   Rš   R4   R…   t   values_listR·   RW   R:   RM   R   R/   R    R“   (
   R   R—   Ri   RÓ   R¡   RR   RŸ   R¤   Rj   t   fk_val(   Rw   (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyR4   Ì  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ª   R"   RÐ   R   RÑ   RP   R   RS   R   RT   Rf   R%   R›   Rš   (   R   Ri   RÓ   RR   t   new_obj(   R­   (    sM   /tmp/pip-unpacked-wheel-BAJOf3/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   RS   R   RT   Rf   R%   Rœ   Rš   (   R   Ri   RR   Rj   t   created(   R­   (    sM   /tmp/pip-unpacked-wheel-BAJOf3/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   RS   R   RT   Rf   R%   R   Rš   (   R   Ri   RR   Rj   Rß   (   R­   (    sM   /tmp/pip-unpacked-wheel-BAJOf3/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__inR…   R‘   RÕ   RÖ   u   pre_addR©   R   R×   u   %s_idu   post_add(#   t   django.db.modelsRà   R4   RM   R   R   RU   R   RO   RQ   RR   Rª   R"   Rµ   R/   R-   Rš   RŽ   RP   RS   R   R…   RÜ   R:   R7   R¹   R   R•   R   R©   R±   R   RØ   RÙ   t   bulk_create(   R   R±   R³   R—   Rà   t   new_idsRj   RÝ   RR   t   valst   obj_id(    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyRÒ     sT    	%=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   R…   R‘   RÕ   RÖ   u
   pre_removeR©   R   R×   u   %s__inu   post_remove(   R4   RM   R   R·   R/   Rš   R   RS   Rª   R   R   R•   R   R   RØ   RÙ   R©   Rf   R)   R»   R…   R7   RW   RÀ   R   RÚ   (   R   R±   R³   R—   RŸ   Rj   RÝ   RR   t   target_model_qst   old_valsR½   (   R­   (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyRÔ   [  s2    	"N(   RI   R]   R-   R   R€   R:   R¥   RÀ   RŠ   R   R)   RC   Rš   R¦   R    R¡   R4   R›   Rœ   R   RÒ   RÔ   (    (   R­   Rw   R©   (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyR­     s0   .				&	
		$					E(    (   R§   Rw   R©   (    (   R­   Rw   R©   sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyR«     s    "ÿ k(   R^   t
   __future__R    R   t   operatorR   t	   django.dbR   R   R   Rá   R   R   t   django.db.models.queryR   t   django.utils.deprecationR   t   django.utils.functionalR	   t   objectR
   R_   Rk   Rv   Rx   R¨   R«   (    (    (    sM   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/related_descriptors.pyt   <module>@   s   ¼ ­E	Æ.