
    Hwg                     0   d Z ddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
 ddlmZmZmZ ddlmZ ddlmZmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZ  G d de      Zd Z G d d      Z G d de      Z  G d d      Z! G d d      Z"d Z# G d de"      Z$d Z%y)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.
    )sync_to_async)
FieldError)DEFAULT_DB_ALIASNotSupportedErrorconnectionsroutertransaction)QWindowsignals)	RowNumber)GreaterThanLessThanOrEqual)QuerySet)DeferredAttribute)
AltersDataresolve_callables)cached_propertyc                       e Zd Zd Zy)ForeignKeyDeferredAttributec                    |j                   j                  | j                  j                        |k7  r6| j                  j	                  |      r| j                  j                  |       ||j                   | j                  j                  <   y N)__dict__getfieldattname	is_cacheddelete_cached_valueselfinstancevalues      b/var/www/horilla/myenv/lib/python3.12/site-packages/django/db/models/fields/related_descriptors.py__set__z#ForeignKeyDeferredAttribute.__set__V   sh      !3!34=$**BVBVC
 JJ**8405$**,,-    N)__name__
__module____qualname__r$    r%   r#   r   r   U   s    6r%   r   c                 d   t        di | d|i}| j                  xs t        }| j                  j                  rt
        |   j                  j                  st        d      | j                  j                  | j                  j                  }}| j                  j                  |      j                         D cg c]  \  }}|	 }	}}t        t               ||	      }
|t        |
|      z  }||t!        |
|      z  }| j                  j#                          | j%                  |      S c c}}w )N__inz`Prefetching from a limited queryset is only supported on backends that support window functions.)using)partition_byorder_byr)   )r
   _dbr   query	is_slicedr   featuressupports_over_clauser   low_mark	high_markget_compilerget_order_byr   r   r   r   clear_limitsfilter)queryset
field_name	instances	predicatedbr4   r5   expr_r.   windows              r#   _filter_prefetch_querysetrB   ^   s   5
|4()45I		))B~~2''<<#1  'nn55x~~7O7O) ( ; ;" ; E R R T
T1D
 
 	*xP[22	 ;;I##%??9%%
s   <D,c                   T    e Zd ZdZd Zed        Zd Zd ZddZ	d Z
dd	Zd
 Zd Zy)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                     || _         y r   )r   )r    field_with_rels     r#   __init__z#ForwardManyToOneDescriptor.__init__   s	    #
r%   c           	      *   t        d| j                  j                  j                  j                  t
        f| j                  j                  j                  | j                  j                  j                  d| j                  j                  dd      S NRelatedObjectDoesNotExist.z.RelatedObjectDoesNotExist)r'   r(   )	typer   remote_fieldmodelDoesNotExistAttributeErrorr'   r(   namer    s    r#   rJ   z4ForwardManyToOneDescriptor.RelatedObjectDoesNotExist   so    
 'ZZ$$**77H"jj..99 JJ$$11JJOO!
 	
r%   c                 8    | j                   j                  |      S r   )r   r   r    r!   s     r#   r   z$ForwardManyToOneDescriptor.is_cached   s    zz##H--r%   c                     | j                   j                  j                  j                  j	                  |      j                         S N)hints)r   rM   rN   _base_manager
db_managerallr    rW   s     r#   get_querysetz'ForwardManyToOneDescriptor.get_queryset   s5    zz&&,,::EEEERVVXXr%   Nc                    || j                         }|j                  |d          | j                  j                  }| j                  j                  }|D ci c]  } ||      | }}| j                  j
                  d   }| j                  j                  }|j                         s"t        | j                  j
                        dk(  r)d|j                  z  |D ch c]  } ||      d    c}i}	nd| j                  j                         z  |i}	 |j                  di |	}|j                  s$|D ]  }
| ||
         }|j                  |
|       ! |||d| j                  j                         dfS c c}w c c}w )Nr   r!      %s__inTFr)   )r\   
_add_hintsr   get_foreign_related_valueget_local_related_valueforeign_related_fieldsrM   	is_hiddenlenrQ   related_query_namer9   multipleset_cached_valueget_cache_name)r    r<   r:   rel_obj_attrinstance_attrinstinstances_dictrelated_fieldrM   r0   rel_objr!   s               r#   get_prefetch_querysetz0ForwardManyToOneDescriptor.get_prefetch_queryset   s   ((*HYq\2zz;;

::@IJ--t3JJ

99!<zz.. !!#s4::+L+L'MQR'R$$%JS&T$}T':1'=&TE
 

 = = ??KE"8??+U+ $$# A),w*?@--gx@A JJ%%'
 	
3 K 'Us   E5E:c                 z    | j                  |      }|j                  | j                  j                  |            S Nr^   )r\   r   r   get_reverse_related_filter)r    r!   qss      r#   
get_objectz%ForwardManyToOneDescriptor.get_object   s3    1vvdjj;;HEFFr%   c                    || S 	 | j                   j                  |      }|_| j                   j                  sI| j                  | j                   j                  j                  d| j                   j                  d      |S # t        $ r d| j                   j                  |      v}|r/|j                  j                  | j                   j                        nd}|r@|j                  |      r/|j                  |      }| j                   j                  |d      }nd}|G|rE| j                  |      }| j                   j                  }|j                  s|j                  ||       | j                   j                  ||       Y hw xY w)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 has no rK   )r   get_cached_valueKeyErrorrc   _metaget_ancestor_linkrN   r   rv   rM   rh   ri   nullrJ   r&   rQ   )r    r!   clsrp   	has_valueancestor_linkancestorrM   s           r#   __get__z"ForwardManyToOneDescriptor.__get__   s_    K
	;jj11(;G6 ?4::??00#'::#3#3#<#<djjooN  N?  	;DJJ$F$Fx$PPI  001A1AB 
 !8!8!B )99(C **55h5M9//(3#zz66 $,, 11'8DJJ''':1	;s   B DF
	F
c                    |t        || j                  j                  j                  j                  j
                        srt        d|d|j                  j                  d| j                  j                  d| j                  j                  j                  j                  j                  d	      ||j                  j                  0t        j                  |j                  |      |j                  _
        |j                  j                  0t        j                  |j                  |      |j                  _
        t        j                  ||      st        d|z        | j                  j                  }|g| j                  j                  |d	      }||j!                  |d       | j                  j"                  D ]  \  }}t%        ||j&                  d        nI| j                  j"                  D ]0  \  }}t%        ||j&                  t)        ||j&                               2 | j                  j!                  ||       | |j*                  s|j!                  ||       yyy)
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
        NCannot assign "": "rK   " must be a "" instance.r^   GCannot assign "%r": the current database router prevents this relation.rx   )
isinstancer   rM   rN   r}   concrete_model
ValueErrorobject_namerQ   _stater>   r   db_for_write	__class__allow_relationr{   ri   related_fieldssetattrr   getattrrh   )r    r!   r"   rM   relatedlh_fieldrh_fields          r#   r$   z"ForwardManyToOneDescriptor.__set__   s    Z4::**0066EE&
  NN..JJOOJJ++1177CC  !!)%+%8%8&&&" ||&"("5"5OOh# ((9  "'( 
 zz..
 = jj11(D1IG
 "--gt<&*jj&?&? :"(("2"2D9:
 '+jj&?&? V"(("2"2GE8CSCS4TUV
 	

##He4
 \%:%:))%: &;r%   c                 f    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.
        )r   r   rN   rQ   rR   s    r#   
__reduce__z%ForwardManyToOneDescriptor.__reduce__H  s&     ))4::??;;;r%   r   )r&   r'   r(   __doc__rG   r   rJ   r   r\   rq   rv   r   r$   r   r)   r%   r#   rD   rD   s   sI    
$ 
 
".Y'
RG
1fJ;X<r%   rD   c                   ,     e Zd ZdZ fdZ fdZ x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                 8   | j                   j                  j                  r|j                         }| j                   j                  j                  }|j
                  j                  D cg c]  }|j                   c}t        fd|D              snD ci c]  }|t        ||       }} |di |}|j                  j                  |j                  _        |j                  j                  |j                  _        |S t        | 9  |      S c c}w c c}w )Nc              3   &   K   | ]  }|v  
 y wr   r)   ).0r   fieldss     r#   	<genexpr>z7ForwardOneToOneDescriptor.get_object.<locals>.<genexpr>h  s     =5u=s   r)   )r   rM   parent_linkget_deferred_fieldsrN   r}   concrete_fieldsr   anyr   r   addingr>   superrv   )	r    r!   deferred	rel_modelr   kwargsobjr   r   s	          @r#   rv   z$ForwardOneToOneDescriptor.get_object]  s    ::""..335H 

//55I1:1P1PQemmQF
 =H==GMNe%5!99NN)&)$,OO$:$:

! ( 2 2


w!(++ R Os   +DDc                    t         |   ||       | j                  j                  r| j                  j                  j
                  r|j                  }|j                  D cg c]  }|j                  r|j                  r| }}|D ]S  }|j                  j                  j                  j                  j                  }|t        ||      nd }t        |||       U y y y c c}w r   )r   r$   r   primary_keyrM   r   r}   r   rN   pkr   r   r   )	r    r!   r"   optsr   inherited_pk_fieldsrel_model_pk_name	raw_valuer   s	           r#   r$   z!ForwardOneToOneDescriptor.__set__p  s    %( ::!!djj&=&=&I&I>>D "11#$$);); # #
 - @$)$6$6$<$<$B$B$E$E$M$M!9>9JGE#45PT  "3Y?@ 'J!#s   "!C )r&   r'   r(   r   rv   r$   __classcell__r   s   @r#   r   r   Q  s    	,&@ @r%   r   c                   N    e Zd ZdZd Zed        Zd Zd ZddZ	ddZ
d	 Zd
 Zy)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                     || _         y r   )r   )r    r   s     r#   rG   z"ReverseOneToOneDescriptor.__init__  s     r%   c           	         t        d| j                  j                  j                  t        f| j                  j
                  j                  | j                  j
                  j                  d| j                  j                  dd      S rI   )	rL   r   related_modelrO   rP   rN   r'   r(   rQ   rR   s    r#   rJ   z3ReverseOneToOneDescriptor.RelatedObjectDoesNotExist  sk     '\\''44nE"ll00;; LL&&33LL%%!
 	
r%   c                 8    | j                   j                  |      S r   )r   r   rT   s     r#   r   z#ReverseOneToOneDescriptor.is_cached  s    ||%%h//r%   c                 ~    | j                   j                  j                  j                  |      j	                         S rV   )r   r   rX   rY   rZ   r[   s     r#   r\   z&ReverseOneToOneDescriptor.get_queryset  s/    ||))77BBBOSSUUr%   Nc                 $   || j                         }|j                  |d          | j                  j                  j                  }| j                  j                  j
                  }|D ci c]  } ||      | }}d| j                  j                  j                  z  |i} |j                  di |}|D ]3  }| ||         }	| j                  j                  j                  ||	       5 |||d| j                  j                         dfS c c}w )Nr   r^   r`   TFr)   )
r\   ra   r   r   rc   rb   rQ   r9   ri   rj   )
r    r<   r:   rk   rl   rm   rn   r0   rp   r!   s
             r#   rq   z/ReverseOneToOneDescriptor.get_prefetch_queryset  s   ((*HYq\2||))AA**DD@IJ--t3JJDLL..333Y?"8??+U+   	CG%l7&;<HLL//B	C LL'')
 	
 Ks   ,Dc                 ~   || S 	 | j                   j                  |      }|C| j                  |j                  j                  d| j                   j                         d      |S # t        $ r |j                  }|d}n| j                   j                  j                  |      }	  | j                  |      j                  di |}| j                   j                  j                  ||       n+# | j                   j                  j                  $ r d}Y nw xY w| j                   j                  ||       Y w xY w)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^   rz   rK   r)   )r   r{   r|   r   r   get_forward_related_filterr\   r   ri   r   rO   rJ   r   r&   get_accessor_name)r    r!   r   rp   
related_pkfilter_argss         r#   r   z!ReverseOneToOneDescriptor.__get__  s-    K
	=ll33H=G$ ?00%%..0N0N0PR 
 N/  	=!J!"ll00KKHUKFd///BFFUUG LL&&77J ||11>> #"G# LL))(G<	=s5   A( (?D<("C1
'D<1%DD<DD<;D<c                    |g| j                   j                  d      }|G| j                   j                         t        || j                   j                  j
                  d       yyt        || j                   j                        slt        d|dj                  j                  d| j                   j                         d| j                   j                  j                  j                  d	      j                  j                  0t        j                  j                   |      j                  _        |j                  j                  0t        j                  |j                         |j                  _        t        j"                  |      st        d	|z        t%        fd
| j                   j                  j&                  D              }t)        | j                   j                  j*                        D ]  \  }}t        ||j,                  ||          ! | j                   j/                  |       | j                   j                  j/                  |       y)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``.
        Nrx   r   r   rK   r   r   r^   r   c              3   J   K   | ]  }t        |j                          y wr   r   r   )r   r   r!   s     r#   r   z4ReverseOneToOneDescriptor.__set__.<locals>.<genexpr>)  s$       %--0s    #)r   r{   r   r   r   rQ   r   r   r   r}   r   r   r   r>   r   r   r   r   tuplerd   	enumeratelocal_related_fieldsr   ri   )r    r!   r"   rp   r   indexr   s    `     r#   r$   z!ReverseOneToOneDescriptor.__set__  s     = ll33Hd3KG" 00: !3!3!8!8$? # E4<<#=#=> NN..LL224LL..44@@  !!)%+%8%8&&&" ||&"("5"5OOh# ((9  "'( 
  !\\//FF J !*$,,*<*<*Q*Q R Auu}}j.?@A
 LL))(E: LL//x@r%   c                 f    t         | j                  j                  | j                  j                  ffS r   )r   r   rN   rQ   rR   s    r#   r   z$ReverseOneToOneDescriptor.__reduce__:  s&    ++T\\->->???r%   r   )r&   r'   r(   r   rG   r   rJ   r   r\   rq   r   r$   r   r)   r%   r#   r   r     sD    

 
 
 0V
2+ZEAN@r%   r   c                   :    e Zd ZdZd Zed        ZddZd Zd Z	y)	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                 4    || _         |j                  | _        y r   )relr   )r    r   s     r#   rG   z#ReverseManyToOneDescriptor.__init__O  s    YY
r%   c                     | j                   j                  }t        |j                  j                  | j                         S r   )r   r   "create_reverse_many_to_one_manager_default_managerr   r    r   s     r#   related_manager_clsz.ReverseManyToOneDescriptor.related_manager_clsS  s4    ..1**44HH
 	
r%   Nc                 ,    || 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)
        )r   )r    r!   r   s      r#   r   z"ReverseManyToOneDescriptor.__get__\  s      K''11r%   c                 :    d| j                   j                         fS )Nzreverse side of a related set)r   r   rR   s    r#   _get_set_deprecation_msg_paramsz:ReverseManyToOneDescriptor._get_set_deprecation_msg_paramsk  s    +HH&&(
 	
r%   c                 :    t        d| j                         z        )Nz@Direct assignment to the %s is prohibited. Use %s.set() instead.)	TypeErrorr   r   s      r#   r$   z"ReverseManyToOneDescriptor.__set__q  s#    N2245
 	
r%   r   )
r&   r'   r(   r   rG   r   r   r   r   r$   r)   r%   r#   r   r   ?  s/     
 
2

r%   r   c                 0     G fdd| t               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                       e Zd Z fdZfdZdZd Zd Zd Z fdZ	d fd	Z
dd	d
Zde_        dd	dZde_         fdZde_        d Zde_         fdZde_        d Zde_         fdZde_        d Zde_        W j(                  j*                  r>dd	dZde_        dd	dZde_        dd	dZde_        dd	dZde_        d Zde_        ddddZde_        ddddZde_         xZS ):create_reverse_many_to_one_manager.<locals>.RelatedManagerc                     t         |           || _        j                  | _        j
                  | _        | j
                  j                  |i| _        y r   )r   rG   r!   r   rN   r   rQ   core_filters)r    r!   r   r   s     r#   rG   zCcreate_reverse_many_to_one_manager.<locals>.RelatedManager.__init__  sB    G$DM**DJDJ!%( ;Dr%   c                    t        | j                  |      }t        |j                        } || j                        S r   )r   rN   r   r   r!   )r    managermanager_classr   s      r#   __call__zCcreate_reverse_many_to_one_manager.<locals>.RelatedManager.__call__  s5    djj'2G>w?P?PRUVM //r%   Tc                     | j                   j                  D ]H  }t        | j                  |j                        $t        d| j                  d|j                   d       y )N"#" needs to have a value for field "z'" before this relationship can be used.)r   rd   r   r!   r   r   )r    r   s     r#   _check_fk_valzHcreate_reverse_many_to_one_manager.<locals>.RelatedManager._check_fk_val  s`    :: 4==%--8@$DMM, -!MM?*QS r%   c           
         | j                   xs+ t        j                  | j                  | j                        }t
        |   j                  j                  }|j                  | j                         | j                   r|j                  | j                         }d|_
         |j                  di | j                  }| j                  j                  D ]>  }t        | j                  |j                         }|	|dk(  s+|s.|j#                         c S  | j                  j$                  rV	 | j                  j&                  }t        | j                  |j                         }| j                  || j                  ii|_        |S # t(        $ ra t+        | j                  j,                  d   j.                  D cg c]"  }t        | j                  |j                         $ nc c}w c}      }Y w xY w)X
            Filter the queryset for the instance this manager is bound to.
            r^   T r)   )r/   r   db_for_readrN   r!   r   r2   !interprets_empty_strings_as_nullsra   r,   _defer_next_filterr9   r   r   rd   r   r   nonemany_to_onetarget_fieldr   r   
path_infostarget_fields_known_related_objects)r    r:   r>   empty_strings_as_nullr   valr   
rel_obj_ids           r#   _apply_rel_filterszMcreate_reverse_many_to_one_manager.<locals>.RelatedManager._apply_rel_filters  s    SV//

T]]SB$/%h88 " 7xx#>>$((3*.H'&x;):):;H:: +dmmU]];;3"91F#==?*+ zz%%
N#'::#:#:L ")8L8L!MJJJT]] ;3/ O "  "' 15

0E0Eb0I0W0W , $DMM<3G3GH "Js   +F 3G,5'G
G,+G,c                     	 | j                   j                  j                  | j                  j                  j                                y # t        t        f$ r Y y w xY wr   )r!   _prefetched_objects_cachepopr   rM   rj   rP   r|   rR   s    r#   _remove_prefetched_objectszUcreate_reverse_many_to_one_manager.<locals>.RelatedManager._remove_prefetched_objects  sN    77;;JJ++::< #H- s   AA
 
AAc                 h   | j                   j                  ,t        | j                   j                  j                  d      	 | j                   j
                  | j                  j                  j                            S # t        t        f$ r" t        | 1         }| j                  |      cY S w xY w)NzQ instance needs to have a primary key value before this relationship can be used.)r!   r   r   r   r&   r   r   rM   rj   rP   r|   r   r\   r   r    r:   r   s     r#   r\   zGcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_queryset  s     }}' }}..77: ;N O 9}}>>JJ++::<  #H- 9 7/1..x889s   :B   .B10B1c                 f   |t         
|          }|j                  |d          |j                  |j                  xs | j                        }| j
                  j                  }| j
                  j                  }|D ci c]  } ||      | }}t        || j
                  j                  |      }|D ]J  }| j
                  j                  |      r| ||         }t        || j
                  j                  |       L | j
                  j                  j                         }	|||d|	dfS c c}w )Nr   r^   F)r   r\   ra   r,   r/   r   rc   rb   rB   rQ   r   r   rM   rj   )r    r<   r:   rk   rl   rm   rn   rp   r!   
cache_namer   s             r#   rq   zPcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_prefetch_queryset  s    7/116~~hll&>dhh?H::==L JJ@@MDMNDmD147NNN04::??IVH $ @zz++G4-l7.CDHGTZZ__h?@ 00??AJ\=%URR Os    D.bulkc                     j                           j                          t        j                   j                   j
                        } fd}|rg }|D ]b  } ||       |j                  j                  s|j                  j                  |k7  rt        d|z        |j                  |j                         d   j                  j                  j                  |      j                  |      j                  di  j                   j"                   j
                  i y t%        j&                  |d      5  |D ]  } ||       |j)                           	 d d d        y # 1 sw Y   y xY w)Nr^   c                     t        | j                        s/t        dj                  j                  j                  d|       t        | j                  j                  j                         y )N'' instance expected, got )	r   rN   r   r}   r   r   r   rQ   r!   )r   r    s    r#   check_and_update_objz\create_reverse_many_to_one_manager.<locals>.RelatedManager.add.<locals>.check_and_update_obj  sU    !#tzz2# !JJ,,88  TZZ__dmm<r%   zA%r instance isn't saved. Use bulk=False or save the object first.pk__inFr,   	savepointr)   )r   r   r   r   rN   r!   r   r   r>   r   appendr   rX   r,   r9   updater   rQ   r	   atomicsave)r    r   objsr>   r  pksr   s   `      r#   addz>create_reverse_many_to_one_manager.<locals>.RelatedManager.add  s;    ++-$$TZZ$--HB	=  'C(-zz((CJJMMR,?(0256  JJsvv&' M

((..r2999ELL 

 !''bEB ## #,S1
## # #s   : E$$E-c                T   K    t        | j                        |d|i d {   S 7 wNr   r   r  r    r   r  s      r#   aaddz?create_reverse_many_to_one_manager.<locals>.RelatedManager.aadd  s'     0txx0$BTBBBB   (&(c                     | j                          | j                  || j                  j                  <   t	        j
                  | j                  | j                        }t        | j                  |      &  di |S Nr^   r)   )
r   r!   r   rQ   r   r   rN   r   rY   creater    r   r>   RelatedManagerr   s      r#   r  zAcreate_reverse_many_to_one_manager.<locals>.RelatedManager.create  s]     &*mmF4::??#$$TZZ$--HB)<DNvNNr%   c                 T   K    t        | j                        di | d {   S 7 wNr)   r   r  r    r   s     r#   acreatezBcreate_reverse_many_to_one_manager.<locals>.RelatedManager.acreate#  s%     3t{{3=f====r  c                     | j                          | j                  || j                  j                  <   t	        j
                  | j                  | j                        }t        | j                  |      &  di |S r  )
r   r!   r   rQ   r   r   rN   r   rY   get_or_creater  s      r#   r"  zHcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_or_create(  s]     &*mmF4::??#$$TZZ$--HB)<KUfUUr%   c                 T   K    t        | j                        di | d {   S 7 wr  r   r"  r  s     r#   aget_or_createzIcreate_reverse_many_to_one_manager.<locals>.RelatedManager.aget_or_create0  s'     :t'9'9:DVDDDDr  c                     | j                          | j                  || j                  j                  <   t	        j
                  | j                  | j                        }t        | j                  |      &  di |S r  )
r   r!   r   rQ   r   r   rN   r   rY   update_or_creater  s      r#   r'  zKcreate_reverse_many_to_one_manager.<locals>.RelatedManager.update_or_create5  s^     &*mmF4::??#$$TZZ$--HB)<NXQWXXr%   c                 T   K    t        | j                        di | d {   S 7 wr  r   r'  r  s     r#   aupdate_or_createzLcreate_reverse_many_to_one_manager.<locals>.RelatedManager.aupdate_or_create=  s'     =t'<'<=GGGGGr  c                T   |sy | j                          | j                  j                  | j                        }t	               }|D ]  }t        || j                        s/t        d| j                  j                  j                  d|      | j                  j                  |      |k(  r|j                  |j                         | j                  j                  j                  j                  |d| j                  d       | j                  | j!                  |      |       y )Nr  r  z is not related to rK   r  )r   r   rb   r!   setr   rN   r   r}   r   rc   r  r   rM   rO   _clearr9   )r    r   r  r   old_idsr   s         r#   removezAcreate_reverse_many_to_one_manager.<locals>.RelatedManager.removeF  s    ""$jj::4==I% C%c4::6' !%

 0 0 < < #  zz99#>#ECFF+"jj55;;HH:=t}}M   DKKwK7>r%   c                T   K    t        | j                        |d|i d {   S 7 wr  r   r/  r  s      r#   aremovezBcreate_reverse_many_to_one_manager.<locals>.RelatedManager.aremove`  s'     7]4;;7IDIIIIr  c                H    | j                          | j                  | |       y r   )r   r-  r    r   s     r#   clearz@create_reverse_many_to_one_manager.<locals>.RelatedManager.cleare  s    ""$D$'r%   c                V   K    t        | j                        |       d {   S 7 w)Nr   r   r5  r4  s     r#   aclearzAcreate_reverse_many_to_one_manager.<locals>.RelatedManager.acleark  s"     6]4::6DAAAAs    )')c                    | j                          t        j                  | j                  | j                        }|j                  |      }|r) |j                  di | j                  j                  d i y t        j                  |d      5  |D ]J  }t        || j                  j                  d        |j                  | j                  j                  g       L 	 d d d        y # 1 sw Y   y xY w)Nr^   Fr  )update_fieldsr)   )r   r   r   rN   r!   r,   r  r   rQ   r	   r  r   r  )r    r:   r   r>   r   s        r#   r-  zAcreate_reverse_many_to_one_manager.<locals>.RelatedManager._clearp  s    //1((dmmL#>>"-#HOO>tzz&=>$++"F F#+ FC#C$?HHDJJOO3DHEFF F Fs   AC))C2F)r   r5  c                   | j                          t        |      }| j                  j                  rt	        j
                  | j                  | j                        }t        j                  |d      5  |r%| j                  |        | j                  |d|i n|t        | j                  |      j                               }g }|D ])  }||v r|j                  |       |j!                  |       +  | j                  |d|i  | j                  |d|i d d d        y  | j                  |d|i y # 1 sw Y   y xY w)Nr^   Fr  r   r   )r   r   r   r   r   r   rN   r!   r	   r  r5  r  r,  r,   rZ   r/  r
  )r    r  r   r5  r>   old_objsnew_objsr   s           r#   r,  z>create_reverse_many_to_one_manager.<locals>.RelatedManager.set  s     ;Dzz((dmmL ''bEB 7


- $2T2#&tzz"~'9'9';#<#%#' 5C"h ( 4 ( 4	5 $X9D9 (667 7  $*T*!7 7s   4B$D44D=c                Z   K    t        | j                        |||       d {   S 7 w)N)r  r   r5  r   r,  )r    r  r   r5  s       r#   asetz?create_reverse_many_to_one_manager.<locals>.RelatedManager.aset  s&     0txx0dUSSSS   "+)+r   )r&   r'   r(   rG   r   do_not_call_in_templatesr   r   r   r\   rq   r  alters_datar  r  r   r"  r%  r'  r*  r   r   r/  r2  r5  r8  r-  r,  r@  r   )r   r  r   s   @r#   r  r     sJ   	<	0
 $( 	&	P		9"	S* #' #	#J )- 	C  	O "	> #	V %)!	E &*"	Y (,$	H )-% 99>>)- ?0 "&F04 J #'G$( ( !%E+/ B "&FF "&F$( 	+4 +/u 	T  r%   r  r   )
superclassr   r  s    `@r#   r   r   x  s    ^ Z ^ @	 r%   c                   J     e Zd ZdZd fd	Zed        Zed        Zd Z	 x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.
    c                 2    t         |   |       || _        y r   )r   rG   reverse)r    r   rI  r   s      r#   rG   zManyToManyDescriptor.__init__  s    r%   c                 .    | j                   j                  S r   )r   throughrR   s    r#   rK  zManyToManyDescriptor.through  s    
 xxr%   c                     | j                   r| j                  j                  n| j                  j                  }t	        |j
                  j                  | j                  | j                         S )N)rI  )rI  r   r   rN   #create_forward_many_to_many_managerr   r   r   s     r#   r   z(ManyToManyDescriptor.related_manager_cls  sK    26,,..DHHNN2**44HHLL
 	
r%   c                     d| j                   rdndz  | j                   r| j                  j                         fS | j                  j                  fS )Nz%s side of a many-to-many setrI  forward)rI  r   r   r   rQ   rR   s    r#   r   z4ManyToManyDescriptor._get_set_deprecation_msg_params  sM    + LLyi9,0LLDHH&&(
 	
 ?Cjjoo
 	
r%   )F)r&   r'   r(   r   rG   propertyrK  r   r   r   r   r   s   @r#   rG  rG    s:     
     
 

r%   rG  c                 4     G fdd| t               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                       e Zd Zd  fd	ZfdZdZd Zd Zd Z fdZ	d  fd		Z
dd
dZde_        dd
dZde_        d Zde_        d Zde_         fdZde_        d Zde_        ddddZde_        ddddZde_        dd
 fd
Zde_        dd
dZde_        dd
 fd
Zde_        dd
dZde_        dd
 fd
Zde_        dd
dZde_        d Zd Zd Zdd
dZ fdZ xZS )!?create_forward_many_to_many_manager.<locals>.ManyRelatedManagerNc                    t         |           || _        sj                  | _        j                  j                         | _        j                  j                  | _        j                  j                         | _
        j                  j                         | _        j                  | _        nj                  | _        j                  j                  | _        j                  j                         | _        j                  j                         | _
        j                  j                         | _        d| _        j                  | _        | _        | j                  j"                  j%                  | j                        | _        | j                  j"                  j%                  | j                        | _        i | _        i | _        | j&                  j.                  D ]f  \  }}| j                  d|j                  }t1        ||j2                        | j*                  |<   |j                  | j,                  |j                  <   h | j&                  j5                  |      | _        d | j6                  v r)t9        d|d| j,                  | j                     d      |j:                  "t9        d|j<                  j>                  z        y )NF__r   r   z4" 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.) r   rG   r!   rN   r   rg   query_field_namerQ   prefetch_cache_namem2m_field_namesource_field_namem2m_reverse_field_nametarget_field_namesymmetricalr   rK  rI  r}   	get_fieldsource_fieldr   r   pk_field_namesr   r   r   rb   related_valr   r   r   r&   )r    r!   r   r   core_filter_keyr   r   rI  s        r#   rG   zHcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.__init__  s@   G$DM YY
(+		(D(D(F%+.99>>(),)A)A)C&),)I)I)K&#&??  ..
(+		%+.99+G+G+I(),)I)I)K&),)A)A)C&#( ;;DL"DL $ 2 2 < <T=S=S TD $ 2 2 < <T=S=S TD "D"$D&*&7&7&F&F C"(.2.C.CX]]"S5<XxGWGW5X!!/25=]]##HMM2C
  $00JJ8TDt'''   !4!4T5K5K!LN  {{" ?((112  #r%   c                    t        | j                  |      }t        |j                        } || j                        S rs   )r   rN   rM  r   r!   )r    r   r   r   rI  s      r#   r   zHcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.__call__  s;    djj'2G?!!3M !$--88r%   Tc                    t        j                  | j                  | j                  fg      }t	        |t
               xs |j                         }|r(|t        j                  | j                   d|fg      z  }| j                  r[t        j                  | j                  | j                  fg      }|r(|t        j                  | j                   d|fg      z  }||z  }|S )Nr+   )	r
   r  rY  r`  r   r   _has_filtersr[  r\  )r    removed_valsfiltersremoved_vals_filterssymmetrical_filterss        r#   _build_remove_filterszUcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._build_remove_filters  s    hh!7!79I9I JKLG |X66U,:S:S:U ! $188)?)?(@&E|%T$UVV&'hh,,d.>.>?@'# ('188!334D9<HI, ' ..Nr%   c                     |j                  | j                         | j                  r|j                  | j                        }d|_         |j                         j                  di | j                  S )r   r^   Tr)   )ra   r!   r/   r,   r   _next_is_stickyr9   r   )r    r:   s     r#   r   zRcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._apply_rel_filters*  s_     7xx#>>$((3*.H'48++-44It7H7HIIr%   c                     	 | j                   j                  j                  | j                         y # t        t
        f$ r Y y w xY wr   )r!   r   r   rW  rP   r|   rR   s    r#   r   zZcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._remove_prefetched_objects4  s<    77;;D<T<TU"H- s   /2 AAc                     	 | j                   j                  | j                     S # t        t        f$ r" t
        |          }| j                  |      cY S w xY wr   )r!   r   rW  rP   r|   r   r\   r   r   s     r#   r\   zLcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_queryset:  sU    9}}>>t?W?WXX"H- 9 7/1..x889s   "& .AAc                    |t         |          }|j                  |d          |j                  |j                  xs | j                        }t        |j                         | j                  |      }| j                  j                  j                  | j                        j                  j                  j                  }t        |j                     j                   j"                  }|j%                  j&                  D ci c]-  }d|j(                  z   ||      d ||j*                        / c}      }|fdfdd| j,                  dfS c c}w )	Nr   r^   _prefetch_related_val_%srK   )selectc                 B     t         fdj                  D              S )Nc              3   P   K   | ]  }t        d |j                  z          yw)ro  Nr   )r   fresults     r#   r   zzcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_queryset.<locals>.<lambda>.<locals>.<genexpr>`  s*      % F$>$JK%s   #&)r   r   )rt  fks   `r#   <lambda>zgcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_queryset.<locals>.<lambda>`  s    u %44%   r%   c                 D     t         fdj                  D              S )Nc              3   j   K   | ]*  }|j                  t        |j                               , y wr   )get_db_prep_valuer   r   )r   rs  
connectionrm   s     r#   r   zzcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_queryset.<locals>.<lambda>.<locals>.<genexpr>d  s1      # ''aii(@*M#s   03)r   rd   )rm   rz  ru  s   `r#   rv  zgcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_queryset.<locals>.<lambda>d  s    U #66#  r%   F)r   r\   ra   r,   r/   rB   rk  rV  rK  r}   r]  rY  rN   db_tabler   r>   ops
quote_nameextrar   r   columnrW  )	r    r<   r:   
join_tableqnrs  rz  ru  r   s	         @@r#   rq   zUcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_querysetA  sA    7/116~~hll&>dhh?H0((*D,A,A9H ##--d.D.DEB00J$X[[1J**B~~
  44	  /ii *~r!((|"55 & H  (( s   2Ethrough_defaultsc                   | j                          t        j                  | j                  | j                        }t        j                  |d      5   | j                  | j                  | j                  g|d|i | j                  r+ | j                  | j                  | j                  g|d|i d d d        y # 1 sw Y   y xY w)Nr^   Fr  r  )r   r   r   rK  r!   r	   r  
_add_itemsrY  r[  r\  )r    r  r  r>   s       r#   r  zCcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.addm  s    ++-$$T\\DMMJB##"> ****  &6	 ###DOO....  *:	  s   A#B??Cc                T   K    t        | j                        |d|i d {   S 7 w)Nr  r  )r    r  r  s      r#   r  zDcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aadd  s3     0txx0(8   r  c                 t    | j                           | j                  | j                  | j                  g|  y r   )r   _remove_itemsrY  r[  r    r  s     r#   r/  zFcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.remove  s2    ++-Dt55t7M7MUPTUr%   c                 N   K    t        | j                        |  d {   S 7 wr   r1  r  s     r#   r2  zGcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aremove  s"     3t{{3T::::s   %#%c           
         t        j                  | j                  | j                        }t	        j
                  |d      5  t        j                  j                  | j                  d| j                  | j                  | j                  d |       | j                          | j                  t        | 9         j                  |            }| j                  j                   j                  |      j#                  |      j%                          t        j                  j                  | j                  d| j                  | j                  | j                  d |       d d d        y # 1 sw Y   y xY w)Nr^   Fr  	pre_clearsenderactionr!   rI  rN   pk_setr,   
post_clear)r   r   rK  r!   r	   r  r   m2m_changedsendrI  rN   r   ri  r   r\   r,   r   r9   delete)r    r>   rf  r   s      r#   r5  zEcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.clear  s   $$T\\DMMJB##"> ##((<<&!]] LL** )  //144UW5I5K5Q5QRT5UV--33B7>>wGNNP##((<<'!]] LL** )   s   DE((E1c                 R   K    t        | j                                d {   S 7 wr   r7  rR   s    r#   r8  zFcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aclear  s      2tzz24444s   '%'F)r5  r  c                   t        |      }t        j                  | j                  | j                        }t        j                  |d      5  |r#| j                           | j                  |d|i nt        | j                  |      j                  | j                  j                  j                  d            }g }|D ]x  }t        || j                        r| j                  j!                  |      d   n| j                  j#                  |      }||v r|j%                  |       h|j'                  |       z  | j$                  |   | j                  |d|i d d d        y # 1 sw Y   y xY w)Nr^   Fr  r  Tflatr   )r   r   r   rK  r!   r	   r  r5  r  r,  r,   values_listr   r   r   rN   rb   get_prep_valuer/  r
  )	r    r  r5  r  r>   r.  r=  r   fk_vals	            r#   r,  zCcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.set  sL    ;D$$T\\DMMJB##"> KJJLDHHdF5EF!

222 --::BB 3 G  "H# 	1  *#tzz: !--GGLQO!%!2!2!A!A#!F 
 "W,#NN62$OOC0	1  DKK)DHHhJ9IJ1K K Ks   DE&&E/c                Z   K    t        | j                        |||       d {   S 7 w)N)r  r5  r  r?  )r    r  r5  r  s       r#   r@  zDcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aset  s0     0txx09I   rA  c                    t        j                  | j                  j                  | j                        }t	        | j                  |        di |}| j                  ||       |S Nr^   r  r)   )r   r   r!   r   r   rY   r  r  )r    r  r   r>   new_objManyRelatedManagerr   s        r#   r  zFcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.create  sW    $$T]]%<%<t}}UB.0CKUfUGHHW/?H@Nr%   c                X   K    t        | j                        dd|i| d {   S 7 wNr  r)   r  r    r  r   s      r#   r   zGcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.acreate  s8     3t{{3 !15;      !*(*c                    t        j                  | j                  j                  | j                        }t	        | j                  |        di |\  }}|r| j                  ||       ||fS r  )r   r   r!   r   r   rY   r"  r  r    r  r   r>   r   createdr  r   s         r#   r"  zMcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_or_create  sj    $$T]]%<%<t}}UB !3T__R5HW LC
 /?@<r%   c                X   K    t        | j                        dd|i| d {   S 7 wr  r$  r  s      r#   r%  zNcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aget_or_create  s:     :t'9'9: !15;   r  c                    t        j                  | j                  j                  | j                        }t	        | j                  |        	 di |\  }}|r| j                  ||       ||fS r  )r   r   r!   r   r   rY   r'  r  r  s         r#   r'  zPcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.update_or_create  so    $$T]]%<%<t}}UB "DOOB$7)!')LC
 /?@<r%   c                X   K    t        | j                        dd|i| d {   S 7 wr  r)  r  s      r#   r*  zQcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aupdate_or_create  s:     =t'<'<= !15;   r  c           
         ddl m} t               }| j                  j                  j                  |      }|D ]  }t        || j                        rt        j                  || j                        sGt        d|d| j                  j                  j                  d|j                  j                  d      |j                  |      d   }|t        d|d|d      |j                  |       t        ||      r/t!        d	| j                  j                  j"                  d
|      |j                  |j%                  |              |S )z[
            Return the set of ids of `objs` that the target field references.
            r   )ModelzCannot add "z": instance is on database "z", value is on database "r   z": the value for field "z	" is Noner  r  )django.db.modelsr  r,  rK  r}   r]  r   rN   r   r   r!   r   r   r>   rb   r  r   r   r  )r    r[  r  r  
target_idsr   r   	target_ids           r#   _get_target_idszOcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._get_target_ids  s%    /J<<--778IJL Ec4::.!00dmmD(  #DMM$8$8$;$;SZZ]]L 
 !- F Fs KA NI (("$57  NN9-U+#::++77> 
 NN<#>#>s#CD+E, r%   c                      | j                   j                  j                  |      j                  |d      j                  di || j
                  d   d|z  |i}|j                  |      S )z{
            Return the subset of ids of `objs` that aren't already assigned to
            this relationship.
            Tr  r   r`   r)   )rK  r   r,   r  r9   r`  
difference)r    rY  r[  r>   r  valss         r#   _get_missing_target_idszWcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._get_missing_target_ids6  sv    --33B7.T: *4+;+;A+> #44j  ((..r%   c                 (   | j                   j                  j                  duxr t        |   j                  j
                  }| j                  xs || j                  k(  xr) t        j                  j                  | j                         }|||xr | 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)rK  r}   auto_createdr   r2   supports_ignore_conflictsrI  rY  r   r  has_listeners)r    r>   rY  can_ignore_conflictsmust_send_signalss        r#   _get_add_planzMcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._get_add_planI  s     ""//u< GO,,FF ! K 1T5K5K K!D&&44T\\B  %!%?.?*? r%   c                j   |sy t        t        |xs i             }| j                  ||      }t        j                  | j
                  | j                        }| j                  ||      \  }}}	|	rl| j
                  j                  j                  |      j                  |D 
cg c]+  }
 | j
                  di d|z  | j                  d   d|z  |
i- c}
d       y | j                  ||||      }t        j                  |d      5  |rNt        j                   j#                  | j
                  d| j                  | j$                  | j&                  ||	       | j
                  j                  j                  |      j                  |D 
cg c]-  }
 | j
                  di |d|z  | j                  d   d|z  |
i/ c}
|       |rNt        j                   j#                  | j
                  d
| j                  | j$                  | j&                  ||	       d d d        y c c}
w c c}
w # 1 sw Y   y xY w)Nr^   z%s_idr   T)ignore_conflictsFr  pre_addr  post_addr)   )dictr   r  r   r   rK  r!   r  r   r,   bulk_creater`  r  r	   r  r   r  r  rI  rN   )r    rY  r[  r  r  r  r>   r  r  can_fast_addr  missing_target_idss               r#   r  zJcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._add_itemsj  sU    #$56F6L"$MN--.?FJ$$T\\DMMJBDHDVDV%EA "3\ --33B7CC *4 & %  '*; ;T=M=Ma=P '*; ;Y &* D  !%!=!=!#4b*" ##"> #$'',,#||(!% $"jj1  -  --33B7CC *<	 & % . !(*; ;T=M=Ma=P '*; ;Y	 &: D  %'',,#||)!% $"jj1  - 7# #!:	# #s&   #0HBH)2H$
=AH)$H))H2c           
      2   |sy t               }|D ]Y  }t        || j                        r0| j                  j	                  |      d   }|j                  |       I|j                  |       [ t        j                  | j                  | j                        }t        j                  |d      5  t        j                  j                  | j                  d| j                  | j                  | j                  ||       t         | E         }|j%                         rE |j'                  |      j(                  d	i d| j                  j                  j*                  z  |i}	n|}	| j-                  |	      }
| j                  j.                  j'                  |      j)                  |
      j1                          t        j                  j                  | j                  d| j                  | j                  | j                  ||       d d d        y # 1 sw Y   y xY w)
Nr   r^   Fr  
pre_remover  r`   post_remover)   )r,  r   rN   r   rb   r  r   r   rK  r!   r	   r  r   r  r  rI  r   r\   rd  r,   r9   r   ri  r   r  )r    rY  r[  r  r.  r   r  r>   target_model_qsold_valsrf  r   s              r#   r  zMcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._remove_items  s   
  eG %c4::.!..HHMaPFKK'KK$% $$T\\DMMJB##"> ##((<<'!]] LL**" )  #('"6"8"//1?44R8??  #d&7&7&D&D&L&LLgV H  'H44X>--33B7>>wGNNP##((<<(!]] LL**" ) +  s   /EHHr   ) r&   r'   r(   rG   r   rB  ri  r   r   r\   rq   r  rC  r  r/  r2  r5  r8  r,  r@  r  r   r"  r%  r'  r*  r  r  r  r  r  r   )r   r  r   rI  s   @r#   r  rS    sS   0	d	9 $( 	(	J		9*	X /3 	( 59 	
  	V "	; #	4 !	5 "%*T 	K@ ,1D 	
  -1 	 "48 	
 #48 		  %)!;? 	
 &*"7; 		  (,$>B 	
 )-%	@	/&	D QUE	N/	 /	r%   r  rD  )rE  r   rI  r  s    ``@r#   rM  rM    s"    F FZ FP r%   N)&r   asgiref.syncr   django.core.exceptionsr   	django.dbr   r   r   r   r	   r  r
   r   r   django.db.models.functionsr   django.db.models.lookupsr   r   django.db.models.queryr   django.db.models.query_utilsr   django.db.models.utilsr   r   django.utils.functionalr   r   rB   rD   r   r   r   r   rG  rM  r)   r%   r#   <module>r     s   ?B ' -  0 / 0 A + : @ 36"3 6&*[< [<|0@ : 0@fx@ x@v6
 6
rhV	,
5 ,
^Pr%   