U
    g-                     @   s   d 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dlmZ G d	d
 d
eZG dd dZG dd deZG dd dZG dd dZdd ZG dd deZdd ZdS )a
  
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.
    )
FieldError)connectionsroutertransaction)Qsignals)QuerySet)DeferredAttribute)resolve_callables)cached_propertyc                   @   s   e Zd Zdd ZdS )ForeignKeyDeferredAttributec                 C   s>   |j | jj|kr,| j|r,| j| ||j | jj< d S N)__dict__getfieldattname	is_cacheddelete_cached_valueselfinstancevalue r   O/tmp/pip-unpacked-wheel-ye1bytdm/django/db/models/fields/related_descriptors.py__set__L   s     z#ForeignKeyDeferredAttribute.__set__N)__name__
__module____qualname__r   r   r   r   r   r   K   s   r   c                   @   s`   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 Z
dddZdd Zdd Zd
S )ForwardManyToOneDescriptoraJ  
    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
   || _ d S r   )r   )r   Zfield_with_relr   r   r   __init___   s    z#ForwardManyToOneDescriptor.__init__c                 C   s6   t d| jjjjtf| jjjd| jjj| jjf dS NRelatedObjectDoesNotExistz%s.%s.RelatedObjectDoesNotExist)r   r   )	typer   remote_fieldmodelDoesNotExistAttributeErrorr   r   namer   r   r   r   r!   b   s    z4ForwardManyToOneDescriptor.RelatedObjectDoesNotExistc                 C   s   | j |S r   )r   r   r   r   r   r   r   r   r   s    z$ForwardManyToOneDescriptor.is_cachedc                 K   s   | j jjjj|d S N)hints)r   r#   r$   _base_manager
db_managerallr   r+   r   r   r   get_querysetu   s    z'ForwardManyToOneDescriptor.get_querysetNc           
         s   |d kr|   }|j|d d | jj}| jj  fdd|D }| jjd }| jj}| snt| jjdkrd|j	  fdd|D i}nd| j
  |i}|jf |}|js|D ]}||| }	|||	 q|| d	| j d
fS )Nr   r   c                    s   i | ]} ||qS r   r   .0instZinstance_attrr   r   
<dictcomp>   s      zDForwardManyToOneDescriptor.get_prefetch_queryset.<locals>.<dictcomp>   %s__inc                    s   h | ]} |d  qS )r   r   r2   r5   r   r   	<setcomp>   s     zCForwardManyToOneDescriptor.get_prefetch_queryset.<locals>.<setcomp>TF)r0   
_add_hintsr   get_foreign_related_valueget_local_related_valueforeign_related_fieldsr#   Z	is_hiddenlenr'   related_query_namefiltermultipleset_cached_valueget_cache_name)
r   	instancesquerysetrel_obj_attrinstances_dictZrelated_fieldr#   queryrel_objr   r   r5   r   get_prefetch_querysetx   s"    z0ForwardManyToOneDescriptor.get_prefetch_querysetc                 C   s   | j |d}|| j|S Nr1   )r0   r   r   Zget_reverse_related_filter)r   r   qsr   r   r   
get_object   s    z%ForwardManyToOneDescriptor.get_objectc                 C   s   |dkr| S z| j |}W n tk
r   d| j |k}|rP|j| j jnd}|r~||r~||}| j j|dd}nd}|dkr|r| |}| j j	}|j
s||| | j || Y nX |dkr| j js| d| j jj| j jf n|S d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)
        Ndefault%s has no %s.)r   get_cached_valueKeyErrorr<   _metaZget_ancestor_linkr$   r   rM   r#   rA   rB   nullr!   r   r'   )r   r   clsrI   Z	has_valueZancestor_linkZancestorr#   r   r   r   __get__   s,    


z"ForwardManyToOneDescriptor.__get__c                 C   sT  |dk	rDt || jjjjjsDtd||jj| jj| jjjjjf n`|dk	r|j	j
dkrltj|j|d|j	_
|j	j
dkrtj|j|d|j	_
t||std| | jj}|dkr| jj|dd}|dk	r||d | jjD ]\}}t||jd qn*| jjD ] \}}t||jt||j q| j|| |dk	rP|jsP||| 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
        N4Cannot assign "%r": "%s.%s" must be a "%s" instance.r1   GCannot assign "%r": the current database router prevents this relation.rN   )
isinstancer   r#   r$   rS   Zconcrete_model
ValueErrorobject_namer'   _statedbr   db_for_write	__class__allow_relationrQ   rB   related_fieldssetattrr   getattrrA   )r   r   r   r#   relatedlh_fieldrh_fieldr   r   r   r      s8    z"ForwardManyToOneDescriptor.__set__c                 C   s   t | jj| jjffS )z
        Pickling should return the instance attached by self.field on the
        model, not a new copy of that descriptor. Use getattr() to retrieve
        the instance directly from the model.
        )rc   r   r$   r'   r(   r   r   r   
__reduce__  s    z%ForwardManyToOneDescriptor.__reduce__)N)N)r   r   r   __doc__r   r   r!   r   r0   rJ   rM   rV   r   rg   r   r   r   r   r   R   s   


/Br   c                       s,   e Zd ZdZ fddZ fddZ  ZS )ForwardOneToOneDescriptora  
    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}dd |jjD  t fdd|D s|fdd D }|f |}jj	|j_	jj
|j_
|S t S )Nc                 S   s   g | ]
}|j qS r   )r   r3   r   r   r   r   
<listcomp>(  s     z8ForwardOneToOneDescriptor.get_object.<locals>.<listcomp>c                 3   s   | ]}| kV  qd S r   r   rj   )fieldsr   r   	<genexpr>-  s     z7ForwardOneToOneDescriptor.get_object.<locals>.<genexpr>c                    s   i | ]}|t  |qS r   )rc   rj   r1   r   r   r6   .  s      z8ForwardOneToOneDescriptor.get_object.<locals>.<dictcomp>)r   r#   parent_linkZget_deferred_fieldsr$   rS   concrete_fieldsanyr\   addingr]   superrM   )r   r   deferredZ	rel_modelkwargsobjr_   )rl   r   r   rM   "  s    


z$ForwardOneToOneDescriptor.get_objectc                    st   t  || | jjrp| jjjrp|j}dd |jD }|D ]4}|jjjj	j
}|d k	r^t||nd }t||| q:d S )Nc                 S   s   g | ]}|j r|jr|qS r   )primary_keyr#   rj   r   r   r   rk   <  s    z5ForwardOneToOneDescriptor.__set__.<locals>.<listcomp>)rr   r   r   rw   r#   rn   rS   ro   r$   pkr   rc   rb   )r   r   r   optsZinherited_pk_fieldsr   Zrel_model_pk_name	raw_valuerv   r   r   r   5  s    z!ForwardOneToOneDescriptor.__set__)r   r   r   rh   rM   r   __classcell__r   r   rv   r   ri     s   ri   c                   @   sX   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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
   || _ d S r   )rd   )r   rd   r   r   r   r   S  s    z"ReverseOneToOneDescriptor.__init__c                 C   s4   t d| jjjtf| jjjd| jjj| jjf dS r    )	r"   rd   related_modelr%   r&   r$   r   r   r'   r(   r   r   r   r!   X  s    z3ReverseOneToOneDescriptor.RelatedObjectDoesNotExistc                 C   s   | j |S r   )rd   r   r)   r   r   r   r   g  s    z#ReverseOneToOneDescriptor.is_cachedc                 K   s   | j jjj|d S r*   )rd   r}   r,   r-   r.   r/   r   r   r   r0   j  s    z&ReverseOneToOneDescriptor.get_querysetNc                    s   |d kr|   }|j|d d | jjj}| jjj  fdd|D }d| jjj |i}|jf |}|D ] }||| }| jj|| qh|| d| j	 dfS )Nr   r1   c                    s   i | ]} ||qS r   r   r2   r5   r   r   r6   t  s      zCReverseOneToOneDescriptor.get_prefetch_queryset.<locals>.<dictcomp>r8   TF)
r0   r:   rd   r   r<   r;   r'   r@   rB   rC   )r   rD   rE   rF   rG   rH   rI   r   r   r5   r   rJ   m  s    

z/ReverseOneToOneDescriptor.get_prefetch_querysetc                 C   s   |dkr| S z| j |}W n tk
r   |j}|dkr@d}nV| j j|}z| j|djf |}W n | j jj	k
r   d}Y nX | j j
|| | j 
|| Y nX |dkr| d|jj| j  f n|S d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``.
        Nr1   rP   )rd   rQ   rR   rx   r   Zget_forward_related_filterr0   r   r}   r%   rB   r!   r_   r   get_accessor_name)r   r   rU   rI   
related_pkZfilter_argsr   r   r   rV     s.    
z!ReverseOneToOneDescriptor.__get__c                    s6  |dkr@| j j dd}|dk	r>| j   t|| j jjd nt|| j jsvtd| j	j
| j  | j jj	j
f n jjdkrtj j|d j_|jjdkrtj|j d|j_t| std| t fdd| j jjD }t| j jjD ]\}}t||j||  q| 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``.
        NrN   rW   r1   rX   c                 3   s   | ]}t  |jV  qd S r   rc   r   rj   r1   r   r   rm     s     z4ReverseOneToOneDescriptor.__set__.<locals>.<genexpr>)rd   rQ   r   rb   r   r'   rY   r}   rZ   rS   r[   r~   r\   r]   r   r^   r_   r`   tupler=   	enumeratelocal_related_fieldsr   rB   )r   r   r   rI   r   indexr   r   r1   r   r     s2    
	z!ReverseOneToOneDescriptor.__set__c                 C   s   t | jj| jjffS r   )rc   rd   r$   r'   r(   r   r   r   rg     s    z$ReverseOneToOneDescriptor.__reduce__)N)N)r   r   r   rh   r   r   r!   r   r0   rJ   rV   r   rg   r   r   r   r   r|   F  s   


/;r|   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 r   )relr   )r   r   r   r   r   r     s    z#ReverseManyToOneDescriptor.__init__c                 C   s   | j j}t|jj| j S r   )r   r}   "create_reverse_many_to_one_manager_default_managerr_   r   r}   r   r   r   related_manager_cls  s
    z.ReverseManyToOneDescriptor.related_manager_clsNc                 C   s   |dkr| S |  |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)r   )r   r   rU   r   r   r   rV     s    
z"ReverseManyToOneDescriptor.__get__c                 C   s   d| j  fS )Nzreverse side of a related set)r   r~   r(   r   r   r   _get_set_deprecation_msg_params  s    z:ReverseManyToOneDescriptor._get_set_deprecation_msg_paramsc                 C   s   t d|   d S )Nz@Direct assignment to the %s is prohibited. Use %s.set() instead.)	TypeErrorr   r   r   r   r   r      s
    z"ReverseManyToOneDescriptor.__set__)N)
r   r   r   rh   r   r   r   rV   r   r   r   r   r   r   r     s   

r   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                       s   e Zd Z 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	dd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rddddZde_ddddZde_dd Zde_dddd d!Zde_  ZS )#z:create_reverse_many_to_one_manager.<locals>.RelatedManagerc                    s2   t    || _j| _j| _| jj|i| _d S r   )rr   r   r   r}   r$   r   r'   core_filtersr)   )r_   r   r   r   r   0  s
    
zCcreate_reverse_many_to_one_manager.<locals>.RelatedManager.__init__c                   s"   t | j|}t|j }|| jS r   )rc   r$   r   r_   r   r   managerZmanager_class)r   r   r   __call__9  s    zCcreate_reverse_many_to_one_manager.<locals>.RelatedManager.__call__Tc                    s   j ptj j jd}t| jj}|j jd  j rD|	 j }d|_
|jf  j} jjD ]2}t j|j}|dks|dkr`|r`|   S q` jjrz jj}W n6 tk
r   t fdd j d jD }Y nX t j|j} j| jii|_|S )X
            Filter the queryset for the instance this manager is bound to.
            r1   TN c                    s   g | ]}t  j|jqS r   )rc   r   r   )r3   target_fieldr(   r   r   rk   X  s   zacreate_reverse_many_to_one_manager.<locals>.RelatedManager._apply_rel_filters.<locals>.<listcomp>)_dbr   Zdb_for_readr$   r   r   featuresZ!interprets_empty_strings_as_nullsr:   using_defer_next_filterr@   r   r   r=   rc   r   noneZmany_to_oner   r   r   Zget_path_infoZtarget_fieldsZ_known_related_objects)r   rE   r]   Zempty_strings_as_nullr   valr   Z
rel_obj_idr   r(   r   _apply_rel_filters?  s*    
zMcreate_reverse_many_to_one_manager.<locals>.RelatedManager._apply_rel_filtersc              	   S   s8   z| j j| jj  W n ttfk
r2   Y nX d S r   )r   _prefetched_objects_cachepopr   r#   rC   r&   rR   r(   r   r   r   _remove_prefetched_objectsa  s    zUcreate_reverse_many_to_one_manager.<locals>.RelatedManager._remove_prefetched_objectsc              	      sH   z| j j| jj  W S  ttfk
rB   t  }| 	| Y S X d S r   )
r   r   r   r#   rC   r&   rR   rr   r0   r   r   rE   rv   r   r   r0   g  s
    
zGcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_querysetNc           	         s   |d krt   }|j|d d ||jp0| j}| jj}| jj  fdd|D }d| jj |i}|j	f |}|D ] }||| }t
|| jj| qv| jj }|| d|dfS )Nr   r1   c                    s   i | ]} ||qS r   r   r2   r5   r   r   r6   w  s      zdcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_prefetch_queryset.<locals>.<dictcomp>r8   F)rr   r0   r:   r   r   r   r<   r;   r'   r@   rb   r#   rC   )	r   rD   rE   rF   rG   rH   rI   r   Z
cache_namerv   r5   r   rJ   n  s    
zPcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_prefetch_querysetbulkc             	      s       tj j jd} fdd}|rg }|D ]8}|| |jjsR|jj|kr^td| |	|j
 q2 jj|j|djf  jj ji n4tj|dd  |D ]}|| |  qW 5 Q R X d S )Nr1   c                    s8   t |  js"td jjj| f t|  jj j d S )N'%s' instance expected, got %r)	rY   r$   r   rS   r[   rb   r   r'   r   )ru   r(   r   r   check_and_update_obj  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.Zpk__inFr   Z	savepoint)r   r   r^   r$   r   r\   rq   r]   rZ   appendrx   r,   r   r@   updater   r'   r   atomicsave)r   r   objsr]   r   Zpksru   r   r(   r   add  s,     z>create_reverse_many_to_one_manager.<locals>.RelatedManager.addc                    s8   | j || jj< tj| j| j d}t | |jf |S rK   )	r   r   r'   r   r^   r$   rr   r-   creater   rt   r]   RelatedManagerr_   r   r   r     s    zAcreate_reverse_many_to_one_manager.<locals>.RelatedManager.createc                    s8   | j || jj< tj| j| j d}t | |jf |S rK   )	r   r   r'   r   r^   r$   rr   r-   get_or_creater   r   r   r   r     s    zHcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_or_createc                    s8   | j || jj< tj| j| j d}t | |jf |S rK   )	r   r   r'   r   r^   r$   rr   r-   update_or_creater   r   r   r   r     s    zKcreate_reverse_many_to_one_manager.<locals>.RelatedManager.update_or_createc                W   s   |sd S | j | j}t }|D ]^}t|| jsFtd| jjj|f | j 	||krd|
|j q | j jjd|| jf q | | j|d| d S )Nr   z%r is not related to %r.r   )r   r;   r   setrY   r$   r   rS   r[   r<   r   rx   r#   r%   _clearr@   )r   r   r   r   old_idsru   r   r   r   remove  s      
zAcreate_reverse_many_to_one_manager.<locals>.RelatedManager.removec                S   s   |  | | d S r   )r   )r   r   r   r   r   clear  s    z@create_reverse_many_to_one_manager.<locals>.RelatedManager.clearc              	   S   s   |    tj| j| jd}||}|r>|jf | jjd i nFt	j
|dd2 |D ]&}t|| jjd  |j| jjgd qRW 5 Q R X d S )Nr1   Fr   )Zupdate_fields)r   r   r^   r$   r   r   r   r   r'   r   r   rb   r   )r   rE   r   r]   ru   r   r   r   r     s    
zAcreate_reverse_many_to_one_manager.<locals>.RelatedManager._clearF)r   r   c             	   S   s   t |}| jjrtj| j| jd}tj|dd |rT| j	|d | j
|d|i n^t| | }g }|D ]"}||kr|| qn|| qn| j|d|i | j
|d|i W 5 Q R X n| j
|d|i d S )Nr1   Fr   r   r   )r   r   rT   r   r^   r$   r   r   r   r   r   r   r   r.   r   r   )r   r   r   r   r]   Zold_objsnew_objsru   r   r   r   r     s     z>create_reverse_many_to_one_manager.<locals>.RelatedManager.set)N)r   r   r   r   r   do_not_call_in_templatesr   r   r0   rJ   r   alters_datar   r   r   r   rT   r   r   r   r   r{   r   r   r   rv   r   r   /  s0   	"r   r   )
superclassr   r   r   r   r   '  s     Hr   c                       sB   e Zd ZdZd fdd	Zedd Zedd Zd	d
 Z	  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  | || _d S r   )rr   r   reverse)r   r   r   rv   r   r   r   
  s    zManyToManyDescriptor.__init__c                 C   s   | j jS r   )r   throughr(   r   r   r   r     s    zManyToManyDescriptor.throughc                 C   s,   | j r| jjn| jj}t|jj| j| j dS )N)r   )r   r   r}   r$   #create_forward_many_to_many_managerr   r_   r   r   r   r   r     s    z(ManyToManyDescriptor.related_manager_clsc                 C   s*   d| j rdnd | j r | j n| jjfS )Nz%s side of a many-to-many setr   Zforward)r   r   r~   r   r'   r(   r   r   r   r      s    z4ManyToManyDescriptor._get_set_deprecation_msg_params)F)r   r   r   rh   r   propertyr   r   r   r   r{   r   r   rv   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                       s0  e Zd Zd, fdd	ZfddZdZdd Zd	d
 Zdd Z fddZ	d- fdd	Z
ddddZde_dd Zde_ fddZde_dddddZde_dd fdd
Zde_dd fdd
Zde_dd fd d!
Zde_d"d# Zd$d% Zd&d' Zddd(d)Z fd*d+Z  ZS ).z?create_forward_many_to_many_manager.<locals>.ManyRelatedManagerNc                    sb  t    || _sTj| _j | _jj| _j	 | _
j | _j| _n<j| _jj| _j | _j | _
j	 | _d| _j| _| _| jj| j
| _| jj| j| _i | _i | _| jjD ]8\}}d| j|jf }t||j| j|< |j| j|j< q| j|| _d | jkrBtd|| j| j
 f |jd kr^td|jj 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.) rr   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   rS   	get_fieldZsource_fieldr   r   Zpk_field_namesra   rc   r   r;   related_valrZ   rx   r_   r   )r   r   re   rf   Zcore_filter_key)r_   r   r   r   r   r   0  sD    



zHcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.__init__c                   s&   t | j|}t|j }|| jdS rK   )rc   r$   r   r_   r   r   )r   r   r   r   r   ^  s    zHcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.__call__Tc                 S   s   t f | j| ji}t|t p$| }|rB|t f d| j |iM }| jr~t f | j| ji}|rv|t f d| j |iM }||O }|S )Nr8   )r   r   r   rY   r   _has_filtersr   r   )r   Zremoved_valsfiltersZremoved_vals_filtersZsymmetrical_filtersr   r   r   _build_remove_filtersd  s    zUcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._build_remove_filtersc                 S   s8   |j | jd | jr || j}d|_| jf | jS )r   r1   T)r:   r   r   r   r   _next_is_stickyr@   r   r   r   r   r   r   t  s
    zRcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._apply_rel_filtersc              	   S   s2   z| j j| j W n ttfk
r,   Y nX d S r   )r   r   r   r   r&   rR   r(   r   r   r   r   ~  s    zZcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._remove_prefetched_objectsc              	      sB   z| j j| j W S  ttfk
r<   t  }| | Y S X d S r   )r   r   r   r&   rR   rr   r0   r   r   rv   r   r   r0     s
    
zLcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_querysetc                    s   |d krt   }|j|d d ||jp0| j}d| j |i}| jf |}| jj	
| jjj	jt|j   jj|jfddjD d}|fdd fd	dd
| jd
fS )Nr   r1   r8   c                    s*   i | ]"}d |j  d |jf qS )_prefetch_related_val_%sz%s.%s)r   columnr3   f)
join_tableqnr   r   r6     s   zicreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_queryset.<locals>.<dictcomp>)selectc                    s   t  fddjD S )Nc                 3   s   | ]}t  d |j V  qdS )r   Nr   r   resultr   r   rm     s   zcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_queryset.<locals>.<lambda>.<locals>.<genexpr>)r   r   r   )fkr   r   <lambda>  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"   | ]}| t|j V  qd S r   )Zget_db_prep_valuerc   r   r   )
connectionr4   r   r   rm     s   r   )r   r=   r4   )r   r   r   r   r     s   F)rr   r0   r:   r   r   r   r   r@   r   rS   r   r   r$   Zdb_tabler   r]   opsZ
quote_nameextrar   r   )r   rD   rE   rH   rv   )r   r   r   r   r   rJ     s(    
	



zUcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_querysetthrough_defaultsc             	   W   sv   |    tj| j| jd}tj|ddD | j| j| j	f|d|i | j
rh| j| j	| jf|d|i W 5 Q R X d S )Nr1   Fr   r   )r   r   r^   r   r   r   r   
_add_itemsr   r   r   )r   r   r   r]   r   r   r   r     s(     zCcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.addc                 W   s"   |    | j| j| jf|  d S r   )r   _remove_itemsr   r   )r   r   r   r   r   r     s    zFcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.removec              
      s   t j| j| jd}tj|dd tjj| jd| j| j	| j
d |d |   | t  |}| jj||  tjj| jd| j| j	| j
d |d W 5 Q R X d S )Nr1   Fr   Z	pre_clearZsenderactionr   r   r$   Zpk_setr   Z
post_clear)r   r^   r   r   r   r   r   m2m_changedsendr   r$   r   r   rr   r0   r   r   r@   delete)r   r]   r   rv   r   r   r     s.            zEcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.clearF)r   r   c          	   	   S   s   t |}tj| j| jd}tj|dd |rH|   | j|d|i nt	| 
|j| jjjdd}g }|D ]J}t|| jr| j|d n
| j|}||kr|| qn|| qn| j|  | j|d|i W 5 Q R X d S )Nr1   Fr   r   TZflatr   )r   r   r^   r   r   r   r   r   r   r   r   values_listr   r   rY   r$   r;   get_prep_valuer   r   )	r   r   r   r   r]   r   r   ru   fk_valr   r   r   r     s$    


zCcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.setc                   s>   t j| jj| jd}t | |jf |}| j||d |S Nr1   r   )r   r^   r   r_   rr   r-   r   r   )r   r   rt   r]   Znew_objManyRelatedManagerr_   r   r   r     s    zFcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.createc                   sJ   t j| jj| jd}t | |jf |\}}|rB| j||d ||fS r   )r   r^   r   r_   rr   r-   r   r   r   r   rt   r]   ru   createdr   r   r   r     s
    zMcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_or_createc                   sJ   t j| jj| jd}t | |jf |\}}|rB| j||d ||fS r   )r   r^   r   r_   rr   r-   r   r   r   r   r   r   r     s
    zPcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.update_or_createc                 S   s   ddl m} t }| jj|}|D ]}t|| jrt	|| j
s^td|| j
jj|jjf ||d }|dkrtd||f || q$t||rtd| jjj|f q$||| q$|S )z[
            Return the set of ids of `objs` that the target field references.
            r   )ModelzHCannot add "%r": instance is on database "%s", value is on database "%s"Nz1Cannot add "%r": the value for field "%s" is Noner   )django.db.modelsr   r   r   rS   r   rY   r$   r   r`   r   rZ   r\   r]   r;   r   r   r[   r   )r   r   r   r   
target_idsr   ru   	target_idr   r   r   _get_target_ids  s6    
zOcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._get_target_idsc                 S   s<   | j j|j|ddjf || jd d| |i}||S )z{
            Return the subset of ids of `objs` that aren't already assigned to
            this relationship.
            Tr   r   r8   )r   r   r   r   r@   r   
difference)r   r   r   r]   r   valsr   r   r   _get_missing_target_ids4  s       zWcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._get_missing_target_idsc                 S   sH   t | jjo| jjjdk	}| js*|| jko6tj	
| j}|||oD| fS )aw  
            Return a boolean triple of the way the add should be performed.

            The first element is whether or not bulk_create(ignore_conflicts)
            can be used, the second whether or not signals must be sent, and
            the third element is whether or not the immediate bulk insertion
            with conflicts ignored can be performed.
            F)r   r   Zsupports_ignore_conflictsr   rS   Zauto_createdr   r   r   r   Zhas_listeners)r   r]   r   can_ignore_conflictsmust_send_signalsr   r   r   _get_add_planA  s    zMcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._get_add_planc             
      s"  |sd S t tpi  |}tj j jd} |\}}}	|	rz jj	|j
 fdd|D dd d S  ||}
tj|dd |rtjj jd j j j|
|d	  jj	|j
 fd
d|
D |d |rtjj jd j j j|
|d	 W 5 Q R X d S )Nr1   c                    s.   g | ]&} j f d   jd d  |iqS z%s_idr   r   r   r3   r   )r   r   r   r   r   rk   j  s     z^create_forward_many_to_many_manager.<locals>.ManyRelatedManager._add_items.<locals>.<listcomp>T)Zignore_conflictsFr   Zpre_addr   c              	      s2   g | ]*} j f d   jd d  |iqS r   r   r   r   r   r   r   r   r   rk   ~  s     Zpost_add)dictr
   r   r   r^   r   r   r   r   r   Zbulk_creater   r   r   r   r   r   r   r$   )r   r   r   r   r   r   r]   r   r   Zcan_fast_addZmissing_target_idsr   r   r   r   ^  sZ               zJcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._add_itemsc              
      s  |sd S t  }|D ]6}t|| jr>| j|d }|| q|| qtj| j| j	d}t
j|dd tjj| jd| j	| j| j||d t  }| r||jf d| jjj |i}	n|}	| |	}
| jj||
  tjj| jd| j	| j| j||d W 5 Q R X d S )	Nr   r1   Fr   Z
pre_remover   r8   Zpost_remove)r   rY   r$   r   r;   r   r   r^   r   r   r   r   r   r   r   r   rr   r0   r   r   r@   r   r   r   r   )r   r   r   r   r   ru   r   r]   Ztarget_model_qsZold_valsr   rv   r   r   r     sJ        
 
    zMcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._remove_items)N)N)r   r   r   r   r   r   r   r   r   r0   rJ   r   r   r   r   r   r   r   r   r   r   r   r   r   r{   r   r   r   r   rv   r   r   /  s6   .
'/r   r   )r   r   r   r   r   r   r   '  s
       	r   N)rh   Zdjango.core.exceptionsr   Z	django.dbr   r   r   r   r   r   Zdjango.db.models.queryr   Zdjango.db.models.query_utilsr	   Zdjango.db.models.utilsr
   Zdjango.utils.functionalr   r   r   ri   r|   r   r   r   r   r   r   r   r   <module>   s$   A E0 )9 S.