
 XŽ                 @   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 Gd
 d   d e  Z Gd d   d e  Z Gd d   d e  Z d d   Z Gd d   d e  Z d d   Z d S)aH
  
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.
    )unicode_literalsN)
attrgetter)connectionsroutertransaction)Qsignals)QuerySet)RemovedInDjango20Warning)cached_propertyc               @   sv   e  Z d  Z d Z d d   Z e d d    Z d d   Z d d	   Z d
 d d  Z	 d
 d d  Z
 d d   Z d
 S)ForwardManyToOneDescriptora!  
    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)fieldget_cache_name
cache_name)selfZfield_with_rel r   Y/home/ubuntu/projects/ifolica/build/django/django/db/models/fields/related_descriptors.py__init__X   s    	z#ForwardManyToOneDescriptor.__init__c             C   s(   t  t d  |  j j j j t f i   S)NRelatedObjectDoesNotExist)typestrr   remote_fieldmodelDoesNotExistAttributeError)r   r   r   r   r   \   s    	z4ForwardManyToOneDescriptor.RelatedObjectDoesNotExistc             C   s   t  | |  j  S)N)hasattrr   )r   instancer   r   r   	is_cachedg   s    z$ForwardManyToOneDescriptor.is_cachedc             K   s   |  j  j j } t | j d d  rj t | j d d  s^ t j d j | j j	  t
 d  n  | j } n	 | j } | j d |  j   S)Nuse_for_related_fieldsF*silence_use_for_related_fields_deprecationzQuse_for_related_fields is deprecated, instead set Meta.base_manager_name on '{}'.   hints)r   r   r   getattr_default_managerwarningswarnformat_metalabelr
   _base_manager
db_managerall)r   r!   related_modelmanagerr   r   r   get_querysetj   s    	z'ForwardManyToOneDescriptor.get_querysetNc       
         s^  | d  k r |  j    } n  | j d | d  |  j j } |  j j     f d d   | D } |  j j d } |  j j j   s t |  j j  d k r i t	   f d d   | D  d | j
 6} n i | d |  j j   6} | j |   } |  j j j sH|  j j j   } x1 | D]& } | | |  }	 t | | |	  qWn  | |   d	 |  j f S)
Nr   r   c                s   i  |  ] } |   |   q Sr   r   ).0inst)instance_attrr   r   
<dictcomp>   s   	 zDForwardManyToOneDescriptor.get_prefetch_queryset.<locals>.<dictcomp>   c             3   s   |  ] }   |  d  Vq d S)r   Nr   )r/   r0   )r1   r   r   	<genexpr>   s    zCForwardManyToOneDescriptor.get_prefetch_queryset.<locals>.<genexpr>z%s__inT)r.   
_add_hintsr   get_foreign_related_valueget_local_related_valueforeign_related_fieldsr   Z	is_hiddenlensetnamerelated_query_namefiltermultipler   setattrr   )
r   	instancesquerysetrel_obj_attrinstances_dictZrelated_fieldqueryrel_obj_cache_namerel_objr   r   )r1   r   get_prefetch_querysetz   s"    *0z0ForwardManyToOneDescriptor.get_prefetch_querysetc             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 |  Yn X| d k r |  j j r |  j d |  j j j |  j j f   n | Sd S)a=  
        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)
        Nr   z%s has no %s.)r"   r   r   r   r7   r.   r=   Zget_reverse_related_filtergetr   r>   r?   r   nullr   r   __name__r;   )r   r   clsrF   valqsr   r   r   __get__   s$    
	%z"ForwardManyToOneDescriptor.__get__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)aX  
        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
        Nz4Cannot assign "%r": "%s.%s" must be a "%s" instance.r   zGCannot assign "%r": the current database router prevents this relation.)
isinstancer   r   r   r'   Zconcrete_model
ValueErrorobject_namer;   _statedbr   db_for_write	__class__allow_relationr"   r   r?   r   related_fieldsattnamer>   )r   r   valuerelatedlh_fieldrh_fieldr   r   r   __set__   s4    +		!!$#z"ForwardManyToOneDescriptor.__set__)rJ   
__module____qualname____doc__r   r   r   r   r.   rG   rN   r]   r   r   r   r   r   K   s   )r   c               @   sv   e  Z d  Z d Z d d   Z e d d    Z d d   Z d d	   Z d
 d d  Z	 d
 d d  Z
 d d   Z d
 S)ReverseOneToOneDescriptora  
    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)rZ   r   r   )r   rZ   r   r   r   r     s    	z"ReverseOneToOneDescriptor.__init__c             C   s%   t  t d  |  j j j t f i   S)Nr   )r   r   rZ   r,   r   r   )r   r   r   r   r     s    	z3ReverseOneToOneDescriptor.RelatedObjectDoesNotExistc             C   s   t  | |  j  S)N)r   r   )r   r   r   r   r   r      s    z#ReverseOneToOneDescriptor.is_cachedc             K   s   |  j  j } t | j d d  rg t | j d d  s[ t j d j | j j  t	 d  n  | j } n	 | j
 } | j d |  j   S)Nr   Fr   zQuse_for_related_fields is deprecated, instead set Meta.base_manager_name on '{}'.r    r!   )rZ   r,   r"   r#   r$   r%   r&   r'   r(   r
   r)   r*   r+   )r   r!   r,   r-   r   r   r   r.   #  s    	z&ReverseOneToOneDescriptor.get_querysetNc       	         s   | d  k r |  j    } n  | j d | d  t |  j j j  } d d       f d d   | D } i | d |  j j j 6} | j |   } |  j j j   } x. | D]& } | | |  } t	 | | |  q W| |   d |  j
 f S)	Nr   r   c             S   s
   |  j    S)N)_get_pk_val)objr   r   r   r1   :  s    zFReverseOneToOneDescriptor.get_prefetch_queryset.<locals>.instance_attrc                s   i  |  ] } |   |   q Sr   r   )r/   r0   )r1   r   r   r2   =  s   	 zCReverseOneToOneDescriptor.get_prefetch_queryset.<locals>.<dictcomp>z%s__inT)r.   r5   r   rZ   r   rX   r;   r=   r   r?   r   )	r   r@   rA   rB   rC   rD   rE   rF   r   r   )r1   r   rG   3  s    z/ReverseOneToOneDescriptor.get_prefetch_querysetc             C   s"  | d k r |  Sy t  | |  j  } Wn t k
 r | j   } | d k rW d } nt |  j j j |  } y |  j d |  j |   } Wn! |  j j	 j
 k
 r d } Yn Xt | |  j j j   |  t | |  j |  Yn X| d k r|  j d | j j |  j j   f   n | Sd S)a  
        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``.
        Nr   z%s has no %s.)r"   r   r   rb   rZ   r   Zget_forward_related_filterr.   rH   r,   r   r?   r   r   rU   rJ   get_accessor_name)r   r   rK   rF   
related_pkZfilter_argsr   r   r   rN   I  s*    		z!ReverseOneToOneDescriptor.__get__c                s  | d k rc y t    |  j  } Wn t k
 r6 YqXt   |  j  t | |  j j j d  nt | |  j j	  s t
 d |   j j |  j j   |  j j	 j j f   nF  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	 rd  j j d k	 rdt j |    sdt
 d |   qdn  t   f d d   |  j j j D  } x: t |  j j j  D]# \ } } t | | j | |  qWt   |  j |  t | |  j j j      d S)a  
        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``.
        Nz4Cannot assign "%r": "%s.%s" must be a "%s" instance.r   zGCannot assign "%r": the current database router prevents this relation.c             3   s!   |  ] } t    | j  Vq d  S)N)r"   rX   )r/   r   )r   r   r   r4     s    z4ReverseOneToOneDescriptor.__set__.<locals>.<genexpr>)r"   r   r   delattrr?   rZ   r   r;   rO   r,   rP   r'   rQ   rd   rR   rS   r   rT   rU   rV   tupler8   	enumeratelocal_related_fieldsrX   r   )r   r   rY   rF   re   indexr   r   )r   r   r]   x  s4    	!!$("z!ReverseOneToOneDescriptor.__set__)rJ   r^   r_   r`   r   r   r   r   r.   rG   rN   r]   r   r   r   r   ra     s   
/ra   c               @   s[   e  Z d  Z d Z d d   Z e d d    Z d d d  Z d	 d
   Z d d   Z	 d S)ReverseManyToOneDescriptora  
    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)relr   )r   rl   r   r   r   r     s    	z#ReverseManyToOneDescriptor.__init__c             C   s"   |  j  j } t | j j |  j   S)N)rl   r,   "create_reverse_many_to_one_managerr#   rU   )r   r,   r   r   r   related_manager_cls  s    	z.ReverseManyToOneDescriptor.related_manager_clsNc             C   s   | d k r |  S|  j  |  S)a9  
        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)rn   )r   r   rK   r   r   r   rN     s    
z"ReverseManyToOneDescriptor.__get__c             C   s   d |  j  j   f S)Nzreverse side of a related set)rl   rd   )r   r   r   r   _get_set_deprecation_msg_params  s    z:ReverseManyToOneDescriptor._get_set_deprecation_msg_paramsc             C   s@   t  j d |  j   t d d |  j |  } | j |  d S)aa  
        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
        zhDirect assignment to the %s is deprecated due to the implicit save() that happens. Use %s.set() instead.
stacklevelr    N)r$   r%   ro   r
   rN   r:   )r   r   rY   r-   r   r   r   r]     s    

z"ReverseManyToOneDescriptor.__set__)
rJ   r^   r_   r`   r   r   rn   rN   ro   r]   r   r   r   r   rk     s   	rk   c                s#   G   f d d   d |       S)z
    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.
    c                   sK  e  Z d  Z     f d d   Z  f d d   Z d Z d d   Z    f d d	   Z d
    f d d  Z d d   Z	 d e	 _
    f d d   Z d e _
    f d d   Z d e _
    f d d   Z d e _
  j j r2d d   Z d e _
 d d   Z d e _
 d d   Z d e _
 n  d d   Z d e _
   S)z:create_reverse_many_to_one_manager.<locals>.RelatedManagerc                sN   t    |   j   | |  _  j |  _  j |  _ i | |  j j 6|  _ d  S)N)superr   r   r,   r   r   r;   core_filters)r   r   )RelatedManagerrU   rl   r   r   r      s
    	zCcreate_reverse_many_to_one_manager.<locals>.RelatedManager.__init__c                s:   t  |  j | j d   } t | j    } | |  j  S)Nr-   )r"   r   poprm   rU   r   )r   kwargsr-   manager_class)rl   r   r   __call__	  s    zCcreate_reverse_many_to_one_manager.<locals>.RelatedManager.__call__Tc             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)zX
            Filter the queryset for the instance this manager is bound to.
            r   N )_dbr   Zdb_for_readr   r   r   featuresZ!interprets_empty_strings_as_nullsr5   usingr=   rr   r   r8   r"   rX   nonepkZ_known_related_objects)r   rA   rS   Zempty_strings_as_nullr   rL   r   r   r   _apply_rel_filters  s    $	#zMcreate_reverse_many_to_one_manager.<locals>.RelatedManager._apply_rel_filtersc                s\   y |  j  j |  j j   SWn: t t f k
 rW t   |   j   } |  j |  SYn Xd  S)N)	r   _prefetched_objects_cacher   r<   r   KeyErrorrq   r.   r~   )r   rA   )rs   rU   r   r   r.   "  s
    zGcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_querysetNc       	         s  | d  k r$ t   |   j   } n  | j d | d  | j | j pM |  j  } |  j j } |  j j     f d d   | D } i | d |  j j 6} | j	 |   } x4 | D], } | | |  } t
 | |  j j |  q W|  j j   } | |   d | f S)Nr   r   c                s   i  |  ] } |   |   q Sr   r   )r/   r0   )r1   r   r   r2   2  s   	 zdcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_prefetch_queryset.<locals>.<dictcomp>z%s__inF)rq   r.   r5   r{   ry   r   r7   r6   r;   r=   r?   r<   )	r   r@   rA   rB   rC   rD   rF   r   r   )rs   rU   )r1   r   rG   )  s    zPcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_prefetch_querysetc          
      s6  | j  d d  } t |  } t j   j d   j }   f d 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	 d
  * x" | D] } | |  | j   qWWd  QXd  S)NbulkTr   c                sQ   t  |    j  s4 t d   j j j |  f   n  t |    j j   j  d  S)Nz'%s' instance expected, got %r)	rO   r   	TypeErrorr'   rQ   r?   r   r;   r   )rc   )r   r   r   check_and_update_objC  s    z\create_reverse_many_to_one_manager.<locals>.RelatedManager.add.<locals>.check_and_update_objzA%r instance isn't saved. Use bulk=False or save the object first.pk__inr{   	savepointF)rt   listr   rT   r   r   rR   ZaddingrS   rP   appendr}   r)   r{   r=   updater   r;   r   atomicsave)r   objsru   r   rS   r   Zpksrc   r   )r   r   add>  s&    
$
z>create_reverse_many_to_one_manager.<locals>.RelatedManager.addc                sM   |  j  | |  j j <t j |  j d |  j  } t   |  j |   j |   S)Nr   )	r   r   r;   r   rT   r   rq   r*   create)r   ru   rS   )rs   rU   r   r   r   ^  s    zAcreate_reverse_many_to_one_manager.<locals>.RelatedManager.createc                sM   |  j  | |  j j <t j |  j d |  j  } t   |  j |   j |   S)Nr   )	r   r   r;   r   rT   r   rq   r*   get_or_create)r   ru   rS   )rs   rU   r   r   r   d  s    zHcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_or_createc                sM   |  j  | |  j j <t j |  j d |  j  } t   |  j |   j |   S)Nr   )	r   r   r;   r   rT   r   rq   r*   update_or_create)r   ru   rS   )rs   rU   r   r   r   j  s    zKcreate_reverse_many_to_one_manager.<locals>.RelatedManager.update_or_createc             _   s   | s
 d  S| j  d d  } |  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)Nr   Tz%r is not related to %r.r   )rt   r   r6   r   r:   r7   r   r}   r   r   r   _clearr=   )r   r   ru   r   rL   old_idsrc   r   r   r   remover  s    	zAcreate_reverse_many_to_one_manager.<locals>.RelatedManager.removec             [   s&   | j  d d  } |  j |  |  d  S)Nr   T)rt   r   )r   ru   r   r   r   r   clear  s    z@create_reverse_many_to_one_manager.<locals>.RelatedManager.clearc             S   s   t  j |  j d |  j } | j |  } | rM | j i d  |  j j 6  n_ t j	 d | d d  E x= | D]5 } t
 | |  j j d   | j d |  j j g  qm WWd  QXd  S)Nr   r{   r   FZupdate_fields)r   rT   r   r   r{   r   r   r;   r   r   r?   r   )r   rA   r   rS   rc   r   r   r   r     s    zAcreate_reverse_many_to_one_manager.<locals>.RelatedManager._clearc       	      [   s7  t  |  } | j d d  } | j d d  } |  j j r t j |  j d |  j } t j	 d | d d   | 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)Nr   Tr   Fr   r{   r   )rg   rt   r   rI   r   rT   r   r   r   r   r   r   r:   r{   r+   r   r   )	r   r   ru   r   r   rS   Zold_objsnew_objsrc   r   r   r   r:     s$    
z>create_reverse_many_to_one_manager.<locals>.RelatedManager.set)rJ   r^   r_   r   rw   do_not_call_in_templatesr~   r.   rG   r   alters_datar   r   r   r   rI   r   r   r   r:   r   )rs   rl   )rU   r   rs     s.   							rs   r   )
superclassrl   r   )rs   rl   r   rm     s    rm   c                   s[   e  Z d  Z d Z d   f d d  Z e d d    Z e d d    Z d	 d
   Z	   S)ManyToManyDescriptora  
    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.
    Fc                s#   t  t |   j |  | |  _ d  S)N)rq   r   r   reverse)r   rl   r   )rU   r   r   r     s    zManyToManyDescriptor.__init__c             C   s
   |  j  j S)N)rl   through)r   r   r   r   r     s    zManyToManyDescriptor.throughc             C   s@   |  j  r |  j j n	 |  j j } t | j j |  j d |  j  S)Nr   )r   rl   r,   r   #create_forward_many_to_many_managerr#   rU   )r   r,   r   r   r   rn     s
    !		z(ManyToManyDescriptor.related_manager_clsc             C   s;   d |  j  r d n d |  j  r. |  j j   n	 |  j j f S)Nz%s side of a many-to-many setr   Zforward)r   rl   rd   r   r;   )r   r   r   r   ro     s    z4ManyToManyDescriptor._get_set_deprecation_msg_params)
rJ   r^   r_   r`   r   propertyr   r   rn   ro   r   r   )rU   r   r     s
   
r   c                s&   G    f d d   d |       S)z
    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.
    c                   sx  e  Z d  Z d      f d d  Z   f d d   Z d Z d d   Z d	 d
   Z    f d d   Z d    f d d  Z	  f d d   Z
 d e
 _  f d d   Z d e _    f d d   Z d e _  f d d   Z d e _    f d d   Z d e _    f d d   Z d e _    f d d   Z d e _ d d   Z    f d d    Z   S)!z?create_forward_many_to_many_manager.<locals>.ManyRelatedManagerNc                s  t    |   j   | |  _  s  j |  _  j j   |  _  j j |  _  j j	   |  _
  j j   |  _  j |  _ nZ  j |  _  j j |  _  j j   |  _  j j   |  _
  j j	   |  _ d |  _  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)NFz%s__%sz\"%r" needs to have a value for field "%s" before this many-to-many relationship can be used.z]%r instance needs to have a primary key value before a many-to-many relationship can be used.)rq   r   r   r   r   r<   query_field_namer;   prefetch_cache_nameZm2m_field_namesource_field_nameZm2m_reverse_field_nametarget_field_namesymmetricalr,   r   r   r'   	get_fieldZsource_fieldtarget_fieldrr   rW   r"   rX   r6   related_valrP   r}   rU   rJ   )r   r   r[   r\   Zcore_filter_key)ManyRelatedManagerrU   rl   r   r   r   r     s<    				zHcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.__init__c                s@   t  |  j | j d   } t | j     } | d |  j  S)Nr-   r   )r"   r   rt   r   rU   r   )r   ru   r-   rv   )rl   r   r   r   rw     s    zHcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.__call__Tc             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)Nz%s__in)r   r   r   rO   r	   _has_filtersr   r   )r   Zremoved_valsfiltersZremoved_vals_filtersZsymmetrical_filtersr   r   r   _build_remove_filters   s    !	zUcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._build_remove_filtersc             S   sG   | j  d |  j  |  j r1 | j |  j  } n  | j   j |  j   S)zX
            Filter the queryset for the instance this manager is bound to.
            r   )r5   r   ry   r{   _next_is_stickyr=   rr   )r   rA   r   r   r   r~   0  s    	zRcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._apply_rel_filtersc                sV   y |  j  j |  j SWn: t t f k
 rQ t   |   j   } |  j |  SYn Xd  S)N)r   r   r   r   r   rq   r.   r~   )r   rA   )r   rU   r   r   r.   9  s
    zLcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_querysetc                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 d    j D  } |  f d d      f d	 d   d
 |  j f S)Nr   r   z%s__inselectc                s9   i  |  ]/ } d       | j   f d | j  q S)z%s.%sz_prefetch_related_val_%s)columnrX   )r/   f)
join_tableqnr   r   r2   V  s   	zicreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_queryset.<locals>.<dictcomp>c                s    t    f d d    j D  S)Nc             3   s%   |  ] } t    d  | j  Vq d S)z_prefetch_related_val_%sN)r"   rX   )r/   r   )resultr   r   r4   [  s   zzcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_queryset.<locals>.<lambda>.<locals>.<genexpr>)rg   ri   )r   )fk)r   r   <lambda>Z  s   zgcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_queryset.<locals>.<lambda>c                s#   t     f d d    j D  S)Nc             3   s-   |  ]# } | j  t  | j     Vq d  S)N)Zget_db_prep_valuer"   rX   )r/   r   )
connectionr0   r   r   r4   _  s   zzcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_queryset.<locals>.<lambda>.<locals>.<genexpr>)rg   r8   )r0   )r   r   )r0   r   r   ^  s   F)rq   r.   r5   r{   ry   r   r   r=   r   r'   r   r   Zdb_tabler   rS   opsZ
quote_nameextrari   r   )r   r@   rA   rD   )r   rU   )r   r   r   r   r   rG   @  s$    		zUcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_querysetc                s     j  j j s: |  j  j } t d | j | j f   n  t j |  j  d |  j } t	 j
 d | d d  C |  j |  j |  j |  |  j r |  j |  j |  j |  n  Wd  QXd  S)NziCannot use add() on a ManyToManyField which specifies an intermediary model. Use %s.%s's Manager instead.r   r{   r   F)r   r'   auto_createdr   	app_labelrQ   r   rT   r   r   r   
_add_itemsr   r   r   )r   r   optsrS   )rl   r   r   r   f  s    	zCcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.addc                sW     j  j j s: |  j  j } t d | j | j f   n  |  j |  j |  j |  d  S)NzlCannot use remove() on a ManyToManyField which specifies an intermediary model. Use %s.%s's Manager instead.)	r   r'   r   r   r   rQ   _remove_itemsr   r   )r   r   r   )rl   r   r   r   x  s    zFcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.removec                s  t  j |  j d |  j } t j d | d d   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   r{   r   FsenderactionZ	pre_clearr   r   pk_setZ
post_clear)r   rT   r   r   r   r   r   m2m_changedsendr   r   r   rq   r.   r{   r#   r=   delete)r   rS   r   )r   rU   r   r   r     s    '"zEcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.clearc       
         sk    j  j j s: |  j  j } t d | j | j f   n  t |  } | j d d  } t j	 |  j  d |  j
 } t j d | d d   | r |  j   |  j |   n t |  j |  j |  j j j d d  } g  } xe | D]] } t | |  j  r|  j j |  d	 n | }	 |	 | k r6| j |	  q | j |  q W|  j |   |  j |   Wd  QXd  S)
NzjCannot set values on a ManyToManyField which specifies an intermediary model. Use %s.%s's Manager instead.r   Fr   r{   r   flatTr   )r   r'   r   r   r   rQ   rg   rt   r   rT   r   r   r   r   r   r:   r{   values_listr   rX   rO   r   r6   r   r   )
r   r   ru   r   r   rS   r   r   rc   fk_val)rl   r   r   r:     s*    
-.zCcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.setc                s   |  j  j j s: |  j  j } t d | j | j f   n  t j |  j j	 d |  j } t
   |  j |   j |   } |  j |  | S)NzlCannot use create() on a ManyToManyField which specifies an intermediary model. Use %s.%s's Manager instead.r   )r   r'   r   r   r   rQ   r   rT   r   rU   rq   r*   r   r   )r   ru   r   rS   Znew_obj)r   rU   r   r   r     s    !zFcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.createc                se   t  j |  j j d |  j } t   |  j |   j |   \ } } | r[ |  j |  n  | | f S)Nr   )r   rT   r   rU   rq   r*   r   r   )r   ru   rS   rc   created)r   rU   r   r   r     s
    'zMcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_or_createc                se   t  j |  j j d |  j } t   |  j |   j |   \ } } | r[ |  j |  n  | | f S)Nr   )r   rT   r   rU   rq   r*   r   r   )r   ru   rS   rc   r   )r   rU   r   r   r     s
    'zPcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.update_or_createc       
         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 d j i   j d  6| d	  6  }	 | t |	  } t j d
 | d d     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     f d d   | D    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)Nr   )ModelzHCannot add "%r": instance is on database "%s", value is on database "%s"z1Cannot add "%r": the value for field "%s" is Nonez'%s' instance expected, got %rr   r   Tz%s__inr{   r   Fr   r   Zpre_addr   r   r   c                s<   g  |  ]2 }   j  i   j d  d  6| d  6   q S)r   z%s_id)r   r   )r/   Zobj_id)r   r   r   r   r   
<listcomp>  s   	z^create_forward_many_to_many_manager.<locals>.ManyRelatedManager._add_items.<locals>.<listcomp>Zpost_add) django.db.modelsr   r:   rO   r   r   rV   r   rP   rR   rS   r   r'   r   r6   r   r   rQ   rT   r#   r{   r   r=   r   r   r   r   r   r   r   r   Zbulk_create)
r   r   r   r   r   Znew_idsrc   r   rS   valsr   )r   r   r   r   r     sT    	%zJcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._add_itemsc                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 d  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)Nr   r   r{   r   Fr   r   Z
pre_remover   r   r   z%s__inZpost_remove)r:   rO   r   r   r6   r   r   rT   r   r   r   r   r   r   r   r   rq   r.   r   r{   r=   rX   r   r#   r   )r   r   r   r   r   rc   r   rS   Ztarget_model_qsZold_valsr   )r   rU   r   r   r   $  s2    	"zMcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._remove_items)rJ   r^   r_   r   rw   r   r   r~   r.   rG   r   r   r   r   r:   r   r   r   r   r   r   )r   rl   r   )rU   r   r     s.   ,	&				$				Er   r   )r   rl   r   r   )r   rl   r   r   r     s    " ar   )r`   
__future__r   r$   operatorr   Z	django.dbr   r   r   r   r   r   Zdjango.db.models.queryr	   Zdjango.utils.deprecationr
   Zdjango.utils.functionalr   objectr   ra   rk   rm   r   r   r   r   r   r   <module>=   s   E.