
    MȂiZ                     R   S r SSKJr  SSKJr  SSKJrJrJrJ	r	J
r
  SSKJrJrJrJr  SSKJr  SSKJr  SSKJr  SS	KJrJr  SS
KJr  SSKJr  SSKJrJr  SSK J!r!   " S S\5      r"S r#S r$ " S S5      r% " S S\%5      r& " S S5      r' " S S5      r(S r) " S S\(5      r*S r+g)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)ManagerQWindowsignals)ColPairs)TupleIn)	RowNumber)GreaterThanLessThanOrEqual)QuerySet)DeferredAttribute)
AltersDataresolve_callables)cached_propertyc                       \ rS rSrS rSrg)ForeignKeyDeferredAttributeW   c                 $   UR                   R                  U R                  R                  5      U:w  a;  U R                  R	                  U5      (       a  U R                  R                  U5        X!R                   U R                  R                  '   g N)__dict__getfieldattname	is_cacheddelete_cached_valueselfinstancevalues      \D:\sksbv-state-backend\venv\Lib\site-packages\django/db/models/fields/related_descriptors.py__set__#ForeignKeyDeferredAttribute.__set__X   sl      !3!34=$**BVBVC
 C
 JJ**8405$**,,-     N)__name__
__module____qualname____firstlineno__r(   __static_attributes__r+   r*   r'   r   r   W   s    6r*   r   c                    [        S0 U S3U0D6nU R                  =(       d    [        nU R                  R                  (       a  [
        U   R                  R                  (       d  [        S5      eU R                  R                  U R                  R                  peU R                  R                  US9R                  5        VVs/ sH  u  pxUPM	     n	nn[        [        5       XS9n
U[        X5      -  nUb  U[!        X5      -  nU R                  R#                  5         U R                  R%                  USS9  U $ s  snnf )N__inz`Prefetching from a limited queryset is only supported on backends that support window functions.)using)partition_byorder_byT)	reuse_allr+   )r   _dbr   query	is_slicedr   featuressupports_over_clauser   low_mark	high_markget_compilerget_order_byr   r   r   r   clear_limitsadd_q)queryset
field_name	instances	predicatedbr<   r=   expr_r5   windows              r'   _filter_prefetch_querysetrJ   `   s   5
|4()45I		))B~~2''<<#1  'nn55x~~7O7O) ( ; ;" ; E R R T
 TWTD T 	 
 	*P[22	 ;;I##% NNd3O
s   D>c              #      #    UnUbH  UR                   R                  U 5      nU(       d  US 4v   g UR                  US 5      nX$4v   UnUb  MG  g g 7fr   )_metaget_ancestor_linkget_cached_value)modelstarting_instancecurrent_instanceancestor_linkancestors        r'   _traverse_ancestorsrT   x   sb     (

&(..@@G"D(( 112BDI((# 
&s   AAAc                   `    \ rS rSrSrS r\S 5       rS rS r	SS jr
S	 rSS
 jrS rS rSrg)ForwardManyToOneDescriptor   a.  
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                     Xl         g r   r   )r$   field_with_rels     r'   __init__#ForwardManyToOneDescriptor.__init__   s    #
r*   c           	      .   [        SU R                  R                  R                  R                  [
        4U R                  R                  R                  U R                  R                  R                  < SU R                  R                  < S3S.5      $ NRelatedObjectDoesNotExist.z.RelatedObjectDoesNotExist)r-   r.   )	typer   remote_fieldrO   DoesNotExistAttributeErrorr-   r.   namer$   s    r'   r_   4ForwardManyToOneDescriptor.RelatedObjectDoesNotExist   so    
 'ZZ$$**77H"jj..99 JJ$$11JJOO!
 	
r*   c                 8    U R                   R                  U5      $ r   )r   r!   r$   r%   s     r'   r!   $ForwardManyToOneDescriptor.is_cached   s    zz##H--r*   c                     U R                   R                  R                  R                  R	                  US9R                  5       $ Nhints)r   rb   rO   _base_manager
db_managerallr$   rn   s     r'   get_queryset'ForwardManyToOneDescriptor.get_queryset   s5    zz&&,,::EEEERVVXXr*   Nc           
         U(       a  [        U5      S:w  a  [        S5      eU(       a  US   OU R                  5       nUR                  US   S9  U R                  R
                  nU R                  R                  nU Vs0 sH  oe" U5      U_M     nnU R                  R                  nU R                  R                   V	s/ sH2  n	UR                  R                  U	R                  5      R                  PM4     n
n	UR                  [        [        UR                   R"                  R$                  U
U
U R                  5      ['        U5      5      5      nUR                  R)                  5         UR*                  (       d$  U H  nXt" U5         nUR-                  X5        M      UUUSU R                  R.                  S4$ s  snf s  sn	f )N   Iquerysets argument of get_prefetch_querysets() should have a length of 1.r   r%   TF)len
ValueErrorrs   
_add_hintsr   get_foreign_related_valueget_local_related_valuerb   foreign_related_fieldsr8   resolve_refre   targetfilterr   r   rO   rL   db_tablelistclear_orderingmultipleset_cached_value
cache_name)r$   rD   	querysetsrB   rel_obj_attrinstance_attrinstinstances_dictrb   r   related_fieldsrel_objr%   s                r'   get_prefetch_querysets1ForwardManyToOneDescriptor.get_prefetch_querysets   s   Y1,  $-9Q<$2C2C2EYq\2zz;;

::@IJ	--t3	Jzz.. ::
: NN&&uzz299: 	 
 ??NN((11""JJ	 ^$

 	%%' $$#),w*?@--g@ $ JJ!!
 	
7 K
s    F>8Gc                 t    U R                  US9nUR                  U R                  R                  U5      5      $ Nrx   )rs   r   r   get_reverse_related_filter)r$   r%   qss      r'   
get_object%ForwardManyToOneDescriptor.get_object   s3    1vvdjj;;HEFFr*   c                 
   Uc  U $  U R                   R                  U5      nUcf  U R                   R                  (       dK  U R                  U R                   R                  R                  < SU R                   R                  < S35      eU$ ! [         a    SnSU R                   R                  U5      ;  nU(       aR  U R                   R                  n[        XQ5       H-  u  pgU(       d  M  U R                   R                  USS9nUc  M-    O   UcP  U(       aI  U R                  U5      nU R                   R                  nUR                  (       d  UR                  X15        U R                   R                  X5         GN_f = f)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 r`   )r   rN   KeyErrorr}   rO   rT   r   rb   r   r   nullr_   r,   re   )	r$   r%   clsr   	has_valuerO   rQ   rS   rb   s	            r'   __get__"ForwardManyToOneDescriptor.__get__   sC    K
	;jj11(;G2 ?4::??00#'::#3#3#<#<djjooN  N;  	;GDJJ$F$Fx$PPI

((2Ee2V.$x #'**"="=hPT"="U".! 3W 9//(3#zz66 $,, 11'DJJ'':-	;s   B AF-FA2FFc                    Ub  [        X R                  R                  R                  R                  R
                  5      (       dv  [        SU< SUR                  R                  < SU R                  R                  < SU R                  R                  R                  R                  R                  < S3	5      eUb  UR                  R                  c.  [        R                  " UR                  US9UR                  l
        UR                  R                  c.  [        R                  " UR                  US9UR                  l
        [        R                  " X!5      (       d  [        SU-  5      eU R                  R                  nUce  U R                  R                  USS	9nUb  UR!                  US5        U R                  R"                   H  u  pV[%        XR&                  S5        M     OHU R                  R"                   H.  u  pV[%        XR&                  [)        X&R&                  5      5        M0     U R                  R!                  X5        Ub$  UR*                  (       d  UR!                  X!5        ggg)
a(  
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 "": "r`   " must be a "" instance.rx   GCannot assign "%r": the current database router prevents this relation.r   )
isinstancer   rb   rO   rL   concrete_modelrz   object_namere   _staterF   r   db_for_write	__class__allow_relationrN   r   r   setattrr    getattrr   )r$   r%   r&   rb   relatedlh_fieldrh_fields          r'   r(   "ForwardManyToOneDescriptor.__set__  s    Z::**0066EE&
 &
  NN..JJOOJJ++1177CC  !!)%+%8%8&&&" ||&"("5"5OOh# ((99  "'( 
 zz..
 = jj11(D1IG
 "--gt<&*jj&?&?""2"2D9 '@
 '+jj&?&?""2"2GECSCS4TU '@
 	

##H4
 \%:%:))%: &;r*   c                 f    [         U R                  R                  U R                  R                  44$ )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   rO   re   rf   s    r'   
__reduce__%ForwardManyToOneDescriptor.__reduce__]  s&     ))4::??;;;r*   rY   r   )r,   r-   r.   r/   __doc__r[   r   r_   r!   rs   r   r   r   r(   r   r0   r+   r*   r'   rV   rV      sI    
$ 
 
".Y-
^G
/bJ;X<r*   rV   c                   8   ^  \ rS rSrSrU 4S jrU 4S jrSrU =r$ )ForwardOneToOneDescriptorif  a  
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                 P  >^ U R                   R                  R                  (       a  UR                  5       nU R                   R                  R                  nUR
                  R                   Vs/ sH  oDR                  PM     snm[        U4S jU 5       5      (       dn  T Vs0 sH  oD[        X5      _M     nnU" S0 UD6nUR                  R                  UR                  l        UR                  R                  UR                  l        U$ [        TU ]9  U5      $ s  snf s  snf )Nc              3   *   >#    U H	  oT;   v   M     g 7fr   r+   ).0r   fieldss     r'   	<genexpr>7ForwardOneToOneDescriptor.get_object.<locals>.<genexpr>~  s     =H5Hs   r+   )r   rb   parent_linkget_deferred_fieldsrO   rL   concrete_fieldsr    anyr   r   addingrF   superr   )	r$   r%   deferred	rel_modelr   kwargsobjr   r   s	          @r'   r   $ForwardOneToOneDescriptor.get_objects  s    ::""..335H 

//55I1:1P1PQ1Pmm1PQF
 =H===GMNve!99vN)&)$,OO$:$:

! ( 2 2


w!(++ R Os   0D%D#c                   > [         TU ]  X5        U R                  R                  (       a  U R                  R                  R
                  (       a  UR                  nUR                   Vs/ sH+  nUR                  (       d  M  UR                  (       d  M)  UPM-     nnU HS  nUR                  R                  R                  R                  R                  nUb  [        X&5      OS n[        XU5        MU     g g g s  snf r   )r   r(   r   primary_keyrb   r   rL   r   rO   pkr    r   r   )	r$   r%   r&   optsr   inherited_pk_fieldsrel_model_pk_name	raw_valuer   s	           r'   r(   !ForwardOneToOneDescriptor.__set__  s    ( ::!!djj&=&=&I&I>>D "11#1E$$ ).);); 1   #
 -$)$6$6$<$<$B$B$E$E$M$M!9>9JGE5PT  Y? - 'J!#s   +C:C:C:r+   )	r,   r-   r.   r/   r   r   r(   r0   __classcell__r   s   @r'   r   r   f  s    
,&@ @r*   r   c                   Z    \ rS rSrSrS r\S 5       rS rS r	SS jr
SS	 jrS
 rS rSrg)ReverseOneToOneDescriptori  a  
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                     Xl         g r   r   )r$   r   s     r'   r[   "ReverseOneToOneDescriptor.__init__  s	     r*   c           	         [        SU R                  R                  R                  [        4U R                  R
                  R                  U R                  R
                  R                  < SU R                  R                  < S3S.5      $ r^   )	ra   r   related_modelrc   rd   rO   r-   r.   re   rf   s    r'   r_   3ReverseOneToOneDescriptor.RelatedObjectDoesNotExist  sk     '\\''44nE"ll00;; LL&&33LL%%!
 	
r*   c                 8    U R                   R                  U5      $ r   )r   r!   ri   s     r'   r!   #ReverseOneToOneDescriptor.is_cached  s    ||%%h//r*   c                 x    U R                   R                  R                  R                  US9R	                  5       $ rl   )r   r   ro   rp   rq   rr   s     r'   rs   &ReverseOneToOneDescriptor.get_queryset  s/    ||))77BBBOSSUUr*   Nc                    U(       a  [        U5      S:w  a  [        S5      eU(       a  US   OU R                  5       nUR                  US   S9  U R                  R
                  R                  nU R                  R
                  R                  nU Vs0 sH  oe" U5      U_M     nnSU R                  R
                  R                  -  U0nUR                  " S0 UD6nUR                  R                  5         U H2  n	Xt" U	5         n
U R                  R
                  R                  X5        M4     UUUSU R                  R                  S4$ s  snf )	Nrv   rw   r   rx   %s__inTFr+   )ry   rz   rs   r{   r   r   r}   r|   re   r   r8   r   r   r   )r$   rD   r   rB   r   r   r   r   r8   r   r%   s              r'   r   0ReverseOneToOneDescriptor.get_prefetch_querysets  s:   Y1,  $-9Q<$2C2C2EYq\2||))AA**DD@IJ	--t3	JDLL..333Y???+U+ 	%%'  G%l7&;<HLL//B   LL##
 	
 Ks   Ec                    Uc  U $  U R                   R                  U5      nUcA  U R                  UR                  R                  < SU R                   R                  < S35      eU$ ! [         a    UR                  5       (       d  SnOU R                   R                  R                  U5      n U R                  US9R                  " S0 UD6nU R                   R                  R                  X15        O,! U R                   R                  R                   a    Sn Of = fU R                   R                  X5         GNf = f)aQ  
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``.
Nrx   r   r`   r+   )r   rN   r   
_is_pk_setr   get_forward_related_filterrs   r   r   r   rc   r_   r   r,   accessor_name)r$   r%   r   r   filter_argss        r'   r   !ReverseOneToOneDescriptor.__get__  s$    K
	=ll33H=G" ?00%%..0J0JL 
 N-  	=&&(("ll00KKHUK"///BFFUUG LL&&77J ||11>> #"G# LL))(<	=s6   A( (AE 0C5&E 5&DE DE ?E c                   ^ Ucd  U R                   R                  TSS9nUbF  U R                   R                  T5        [        X0R                   R                  R
                  S5        gg[        X R                   R                  5      (       dl  [        SU< STR                  R                  < SU R                   R                  < SU R                   R                  R                  R                  < S3	5      eTR                  R                  c.  [        R                  " TR                   US9TR                  l        UR                  R                  c.  [        R                  " UR                   TS9UR                  l        [        R"                  " UT5      (       d  [        S	U-  5      e[%        U4S
 jU R                   R                  R&                   5       5      n[)        U R                   R                  R*                  5       H  u  pV[        X&R,                  XE   5        M     U R                   R/                  TU5        U R                   R                  R/                  UT5        g)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``.
Nr   r   r   r`   r   r   rx   r   c              3   N   >#    U H  n[        TUR                  5      v   M     g 7fr   )r   r    )r   r   r%   s     r'   r   4ReverseOneToOneDescriptor.__set__.<locals>.<genexpr>F  s&      FE %--00Fs   "%)r   rN   r"   r   r   re   r   r   rz   rL   r   r   r   rF   r   r   r   r   tupler~   	enumeratelocal_related_fieldsr    r   )r$   r%   r&   r   
related_pkindexr   s    `     r'   r(   !ReverseOneToOneDescriptor.__set__  s   " = ll33Hd3KG" 00: !3!3!8!8$? # E<<#=#=>> NN..LL..LL..44@@  !!)%+%8%8&&&" ||&"("5"5OOh# ((99  "'( 
  !\\//FF J !*$,,*<*<*Q*Q R}}j.?@ !S
 LL))(E:
 LL//x@r*   c                 f    [         U R                  R                  U R                  R                  44$ r   )r   r   rO   re   rf   s    r'   r   $ReverseOneToOneDescriptor.__reduce__X  s&    ++T\\->->???r*   r   r   )r,   r-   r.   r/   r   r[   r   r_   r!   rs   r   r   r(   r   r0   r+   r*   r'   r   r     sE    

 
 
 0V
@*XGAR@r*   r   c                   D    \ rS rSrSrS r\S 5       rS
S jrS r	S r
S	rg)ReverseManyToOneDescriptori]  a  
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_reverse_many_to_one_manager()`` defined below.
c                 2    Xl         UR                  U l        g r   )relr   )r$   r   s     r'   r[   #ReverseManyToOneDescriptor.__init__m  s    YY
r*   c                     U R                   R                  n[        UR                  R                  U R                   5      $ r   )r   r   "create_reverse_many_to_one_manager_default_managerr   r$   r   s     r'   related_manager_cls.ReverseManyToOneDescriptor.related_manager_clsq  s4    ..1**44HH
 	
r*   Nc                 .    Uc  U $ U R                  U5      $ )a	  
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   "ReverseManyToOneDescriptor.__get__z  s      K''11r*   c                 2    SU R                   R                  4$ )Nzreverse side of a related set)r   r   rf   s    r'   _get_set_deprecation_msg_params:ReverseManyToOneDescriptor._get_set_deprecation_msg_params  s    +HH""
 	
r*   c                 :    [        SU R                  5       -  5      e)Nz@Direct assignment to the %s is prohibited. Use %s.set() instead.)	TypeErrorr  r#   s      r'   r(   "ReverseManyToOneDescriptor.__set__  s#    N2245
 	
r*   )r   r   r   )r,   r-   r.   r/   r   r[   r   r   r   r  r(   r0   r+   r*   r'   r   r   ]  s/     
 
2

r*   r   c                 2   ^^  " UU4S jSU [         5      mT$ )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                   *  >^  \ rS rSrU U4S jrU4S jrSrS rS rS r	U 4S jr
SU 4S	 jjrSS
.S jrS\l        SS
.S jrS\l        UU 4S jrS\l        S rS\l        UU 4S jrS\l        S rS\l        UU 4S jrS\l        S rS\l         YR*                  R,                  (       aB  SS
.S jrS\l        SS
.S jrS\l        SS
.S jrS\l        SS
.S jrS\l        S rS\l        SSS.S jrS\l        SSS.S jrS\l        SrU =r$ ):create_reverse_many_to_one_manager.<locals>.RelatedManageri  c                    > [         TU ]  5         Xl        TR                  U l        TR
                  U l        U R
                  R                  U0U l        g r   )r   r[   r%   r   rO   r   re   core_filters)r$   r%   r   r   s     r'   r[   Ccreate_reverse_many_to_one_manager.<locals>.RelatedManager.__init__  s@    G$M**DJDJ!%( ;Dr*   c                   > [        U R                  U5      n[        UR                  T5      nU" U R                  5      $ r   )r   rO   r   r   r%   )r$   managermanager_classr   s      r'   __call__Ccreate_reverse_many_to_one_manager.<locals>.RelatedManager.__call__  s5    djj'2G>w?P?PRUVM //r*   Tc                     U R                   R                   HK  n[        U R                  UR                  5      b  M&  [        SU R                  < SUR                   S35      e   g )N"#" needs to have a value for field "z'" before this relationship can be used.)r   r~   r   r%   r    rz   )r$   r   s     r'   _check_fk_valHcreate_reverse_many_to_one_manager.<locals>.RelatedManager._check_fk_val  s[    ::4==%--8@$DMM, -!MM?*QS  ;r*   c           
      
   U R                   =(       d)    [        R                  " U R                  U R                  S9n[
        U   R                  R                  nUR                  U R                  S9  U R                   (       a  UR                  U R                   5      nSUl
        UR                  " S0 U R                  D6nU R                  R                   HG  n[        U R                  UR                   5      nUb  US:X  d  M.  U(       d  M7  UR#                  5       s  $    U R                  R$                  (       aU   U R                  R&                  n[        U R                  UR                   5      nU R                  XpR                  00Ul        U$ ! [(         ac    [+        U R                  R,                  S   R.                   Vs/ sH#  n[        U R                  UR                   5      PM%     Os  snf sn5      n Nf = f)@
Filter the queryset for the instance this manager is bound to.
rx   T r+   )r7   r   db_for_readrO   r%   r   r:   !interprets_empty_strings_as_nullsr{   r3   _defer_next_filterr   r  r   r~   r   r    nonemany_to_onetarget_fieldr   r   
path_infostarget_fields_known_related_objects)r$   rB   rF   empty_strings_as_nullr   valr!  
rel_obj_ids           r'   _apply_rel_filtersMcreate_reverse_many_to_one_manager.<locals>.RelatedManager._apply_rel_filters  s    SV//

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

0E0Eb0I0W0W0W $DMM<3G3GH0W"Js   ?F 4H	)G3
2HHc                      U R                   R                  R                  U R                  R                  R
                  5        g ! [        [        4 a     g f = fr   )r%   _prefetched_objects_cachepopr   rb   r   rd   r   rf   s    r'   _remove_prefetched_objectsUcreate_reverse_many_to_one_manager.<locals>.RelatedManager._remove_prefetched_objects  sK    77;;JJ++66 #H- s   AA AAc                 v  > U R                   R                  5       (       d-  [        U R                   R                  R                  < S35      e U R                   R
                  U R                  R                  R                     $ ! [        [        4 a"    [        TU ]1  5       nU R                  U5      s $ f = f)NzQ instance needs to have a primary key value before this relationship can be used.)r%   r   rz   r   r,   r+  r   rb   r   rd   r   r   rs   r(  )r$   rB   r   s     r'   rs   Gcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_queryset  s     ==++-- }}..77: ;N O 9}}>>JJ++66  #H- 9 7/1..x889s   6B /B87B8c                   > U(       a  [        U5      S:w  a  [        S5      eU(       a  US   O[        TU ]  5       nUR	                  US   S9  UR                  UR                  =(       d    U R                  5      nU R                  R                  nU R                  R                  nU Vs0 sH  oe" U5      U_M     nn[        X0R                  R                  U5      nU HJ  nU R                  R                  U5      (       a  M%  Xt" U5         n	U R                  R                  X5        ML     U R                  R                  R                  n
X4USU
S4$ s  snf )Nrv   rw   r   rx   F)ry   rz   r   rs   r{   r3   r7   r   r}   r|   rJ   re   r!   r   rb   r   )r$   rD   r   rB   r   r   r   r   r   r%   r   r   s              r'   r   Qcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_prefetch_querysets  s*   S^q0 #  (1y|eg6J6LH16~~hll&>dhh?H::==L JJ@@MDMNIDmD147INN0::??IVH $zz++G44-l7.CDHJJ//B $ 00;;J=%URR Os   -Ebulkc                  ^  T R                  5         T R                  5         [        R                  " T R                  T R
                  S9nU 4S jnU(       a  / nU Hi  nU" U5        UR                  R                  (       d  UR                  R                  U:w  a  [        SU-  5      eUR                  UR                  5        Mk     T R                  R                  R                  U5      R                  US9R                  " S0 T R                   R"                  T R
                  0D6  g [$        R&                  " USS9   U H  nU" U5        UR)                  5         M     S S S 5        g ! , (       d  f       g = f)Nrx   c                    > [        U TR                  5      (       d1  [        STR                  R                  R                  < SU < 35      e[        U TR                  R                  TR                  5        g )N'' instance expected, got )	r   rO   r  rL   r   r   r   re   r%   )r   r$   s    r'   check_and_update_obj\create_reverse_many_to_one_manager.<locals>.RelatedManager.add.<locals>.check_and_update_obj  sX    !#tzz22# !JJ,,88  TZZ__dmm<r*   zA%r instance isn't saved. Use bulk=False or save the object first.pk__inFr3   	savepointr+   )r  r-  r   r   rO   r%   r   r   rF   rz   appendr   ro   r3   r   updater   re   r	   atomicsave)r$   r4  objsrF   r9  pksr   s   `      r'   add>create_reverse_many_to_one_manager.<locals>.RelatedManager.add  s&    ++-$$TZZ$--HB	= C(-zz((CJJMMR,?(0256  JJsvv&   

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

 !''bEB#,S1
  $ CBBs   "E,,
E:c                T   #    [        U R                  5      " USU06I S h  vN $  N7fNr4  r   rE  r$   r4  rC  s      r'   aadd?create_reverse_many_to_one_manager.<locals>.RelatedManager.aadd7  s$     &txx0$BTBBBB   (&(c                   > U R                  5         U R                  5         U R                  XR                  R                  '   [
        R                  " U R                  U R                  S9n[        TU R                  U5      ]*  " S0 UD6$ Nrx   r+   )r  r-  r%   r   re   r   r   rO   r   rp   creater$   r   rF   RelatedManagerr   s      r'   rP  Acreate_reverse_many_to_one_manager.<locals>.RelatedManager.create<  sg     ++-&*mmF::??#$$TZZ$--HB)<DNvNNr*   c                 T   #    [        U R                  5      " S0 UD6I S h  vN $  N7fNr+   r   rP  r$   r   s     r'   acreateBcreate_reverse_many_to_one_manager.<locals>.RelatedManager.acreateE  s"     &t{{3=f====rM  c                    > U R                  5         U R                  XR                  R                  '   [        R
                  " U R                  U R                  S9n[        TU R                  U5      ]&  " S0 UD6$ rO  )
r  r%   r   re   r   r   rO   r   rp   get_or_createrQ  s      r'   r[  Hcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_or_createJ  s[     &*mmF::??#$$TZZ$--HB)<KUfUUr*   c                 T   #    [        U R                  5      " S0 UD6I S h  vN $  N7frU  r   r[  rW  s     r'   aget_or_createIcreate_reverse_many_to_one_manager.<locals>.RelatedManager.aget_or_createR  s$     &t'9'9:DVDDDDrM  c                    > U R                  5         U R                  XR                  R                  '   [        R
                  " U R                  U R                  S9n[        TU R                  U5      ]&  " S0 UD6$ rO  )
r  r%   r   re   r   r   rO   r   rp   update_or_createrQ  s      r'   rb  Kcreate_reverse_many_to_one_manager.<locals>.RelatedManager.update_or_createW  s\     &*mmF::??#$$TZZ$--HB)<NXQWXXr*   c                 T   #    [        U R                  5      " S0 UD6I S h  vN $  N7frU  r   rb  rW  s     r'   aupdate_or_createLcreate_reverse_many_to_one_manager.<locals>.RelatedManager.aupdate_or_create_  s$     &t'<'<=GGGGGrM  c                n   U(       d  g U R                  5         U R                  R                  U R                  5      n[	        5       nU H  n[        XPR                  5      (       d1  [        SU R                  R                  R                  < SU< 35      eU R                  R                  U5      U:X  a  UR                  UR                  5        M  U R                  R                  R                  R                  U< SU R                  < S35      e   U R                  U R!                  US9U5        g )Nr7  r8  z is not related to r`   r;  )r  r   r|   r%   setr   rO   r  rL   r   r}   rE  r   rb   rc   _clearr   )r$   r4  rC  r&  old_idsr   s         r'   removeAcreate_reverse_many_to_one_manager.<locals>.RelatedManager.removeh  s    ""$jj::4==I%C%c::66' !%

 0 0 < < #  zz99#>#ECFF+"jj55;;HH:=t}}M     DKKwK7>r*   c                T   #    [        U R                  5      " USU06I S h  vN $  N7frH  r   rl  rJ  s      r'   aremoveBcreate_reverse_many_to_one_manager.<locals>.RelatedManager.aremove  s$     *4;;7IDIIIIrM  c                F    U R                  5         U R                  X5        g r   )r  rj  r$   r4  s     r'   clear@create_reverse_many_to_one_manager.<locals>.RelatedManager.clear  s    ""$D'r*   c                P   #    [        U R                  5      " US9I S h  vN $  N7f)Nr3  r   rt  rs  s     r'   aclearAcreate_reverse_many_to_one_manager.<locals>.RelatedManager.aclear  s     *4::6DAAAAs   &$&c                    U R                  5         [        R                  " U R                  U R                  S9nUR                  U5      nU(       a)  UR                  " S0 U R                  R                  S 0D6  g [        R                  " USS9   U HG  n[        X@R                  R                  S 5        UR                  U R                  R                  /S9  MI     S S S 5        g ! , (       d  f       g = f)Nrx   Fr=  )update_fieldsr+   )r-  r   r   rO   r%   r3   r@  r   re   r	   rA  r   rB  )r$   rB   r4  rF   r   s        r'   rj  Acreate_reverse_many_to_one_manager.<locals>.RelatedManager._clear  s    //1((dmmL#>>"-OO>tzz&=>$++"F#+C#C$?HHDJJOO3DHE $, GFFs   AC''
C5F)r4  rt  c                   U R                  5         [        U5      nU R                  R                  (       a  [        R
                  " U R                  U R                  S9n[        R                  " USS9   U(       a"  U R                  US9  U R                  " USU06  O[        U R                  U5      R                  5       5      n/ nU H,  nXu;   a  UR                  U5        M  UR!                  U5        M.     U R                  " USU06  U R                  " USU06  S S S 5        g U R                  " USU06  g ! , (       d  f       g = f)Nrx   Fr=  r3  r4  )r  r   r   r   r   r   rO   r%   r	   rA  rt  rE  ri  r3   rq   rl  r?  )r$   rC  r4  rt  rF   old_objsnew_objsr   s           r'   ri  >create_reverse_many_to_one_manager.<locals>.RelatedManager.set  s     ;Dzz((dmmL ''bEB


-$2T2#&tzz"~'9'9';#<#%#'C" ( 4 ( 4	 $( X9D9(66 CB  $*T*! CBs   5B*D;;
E	c                R   #    [        U R                  5      " XUS9I S h  vN $  N7f)N)rC  r4  rt  r   ri  )r$   rC  r4  rt  s       r'   aset?create_reverse_many_to_one_manager.<locals>.RelatedManager.aset  s!     &txx0dUSSSS   '%')r  r   r%   rO   r   ) r,   r-   r.   r/   r[   r  do_not_call_in_templatesr  r(  r-  rs   r   rE  alters_datarK  rP  rX  r[  r_  rb  rf  r   r   rl  rp  rt  rx  rj  ri  r  r0   r   )r   rR  r   s   @r'   rR  r
    sJ   	<	0
 $( 	&	P		9"	S0 #' #	#J )- 	C  	O "	> #	V %)!	E &*"	Y (,$	H )-% 99>>)- ?0 "&F04 J #'G$( ( !%E+/ B "&FF "&F$( 	+4 +/u 	T  r*   rR  r   )
superclassr   rR  s    `@r'   r   r     s"    b  b Z b H	 r*   c                   V   ^  \ rS rSrSrSU 4S jjr\S 5       r\S 5       r	S r
SrU =r$ )	ManyToManyDescriptori  a  
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                 0   > [         TU ]  U5        X l        g r   )r   r[   reverse)r$   r   r  r   s      r'   r[   ManyToManyDescriptor.__init__  s    r*   c                 .    U R                   R                  $ r   )r   throughrf   s    r'   r  ManyToManyDescriptor.through  s    
 xxr*   c                     U R                   (       a  U R                  R                  OU R                  R                  n[	        UR
                  R                  U R                  U R                   S9$ )Nr  )r  r   r   rO   #create_forward_many_to_many_managerr   r   r   s     r'   r   (ManyToManyDescriptor.related_manager_cls  sK    26,,..DHHNN2**44HHLL
 	
r*   c                     SU R                   (       a  SOS-  U R                   (       a  U R                  R                  4$ U R                  R                  4$ )Nz%s side of a many-to-many setr  forward)r  r   r   r   re   rf   s    r'   r  4ManyToManyDescriptor._get_set_deprecation_msg_params  sI    + LLyi9&*llDHH""
 	
 9=


 	
r*   r  )F)r,   r-   r.   r/   r   r[   propertyr  r   r   r  r0   r   r   s   @r'   r  r    s?     
     
 

 
r*   r  c                 :   ^ ^^^  " UUUU 4S jST [         5      mT$ )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                   f  >^  \ rS rSrS&U UU4S jjrUU4S jrSrS rS rU4S jr	U4S	 jr
U 4S
 jrS&U 4S jjr\S 5       rU U4S jrU U4S jrSS.S jrS\l        SS.S jrS\l        S rS\l        S rS\l        U 4S jrS\l        S rS\l        SSS.S jrS\l        SSS.S jrS\l        SS.UU 4S jjrS\l        SS.S jrS\l        SS.UU 4S jjrS\l        SS.S jrS\l        SS.UU 4S jjrS\l        SS.S jrS\l        S  r S! r!S" r"SS.S# jr#U 4S$ jr$S%r%U =r&$ )'?create_forward_many_to_many_manager.<locals>.ManyRelatedManageri  Nc                   > [         TU ]  5         Xl        T(       d  TR                  U l        TR                  R                  5       U l        TR                  R                  U l        TR                  R                  5       U l
        TR                  R                  5       U l        TR                  U l        OTR                  U l        TR                  R                  U l        TR                  R                  5       U l        TR                  R                  5       U l
        TR                  R                  5       U l        SU l        TR                  U l        TU l        U R                  R"                  R%                  U R                  5      U l        U R                  R"                  R%                  U R                  5      U l        0 U l        0 U l        U R&                  R.                   Hg  u  p#U R                  < SUR                  < 3n[1        XR2                  5      U R*                  U'   UR                  U R,                  UR                  '   Mi     U R&                  R5                  U5      U l        S U R6                  ;   a+  [9        SU< SU R,                  U R                     < S35      eUR;                  5       (       d"  [9        SUR<                  R>                  -  5      eg )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   r[   r%   rO   r   related_query_namequery_field_namere   prefetch_cache_namem2m_field_namesource_field_namem2m_reverse_field_nametarget_field_namesymmetricalr   r  r  rL   	get_fieldsource_fieldr!  r  pk_field_namesr   r   r    r|   related_valrz   r   r   r,   )r$   r%   r   r   core_filter_keyr   r   r  s        r'   r[   Hcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.__init__  s9   G$M 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".2.C.CX]]"S5<XGWGW5X!!/25=]]##HMM2 'G
  $00JJ8TDt'''   !4!4T5K5K!LN  &&(( ?((112  )r*   c                ~   > [        U R                  U5      n[        UR                  TT5      nU" U R                  S9$ r   )r   rO   r  r   r%   )r$   r  r  r   r  s      r'   r  Hcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.__call__/  s;    djj'2G?!!3M !$--88r*   Tc                    [         R                  " U R                  U R                  4/5      n[	        U[
        5      (       + =(       d    UR                  5       nU(       a)  U[         R                  " U R                   S3U4/5      -  nU R                  (       aa  [         R                  " U R                  U R                  4/5      nU(       a)  U[         R                  " U R                   S3U4/5      -  nX$-  nU$ )Nr2   )	r   rP  r  r  r   r   _has_filtersr  r  )r$   removed_valsfiltersremoved_vals_filterssymmetrical_filterss        r'   _build_remove_filtersUcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._build_remove_filters8  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                     UR                  U R                  S9  U R                  (       a  UR                  U R                  5      nSUl        UR                  5       R                  " S0 U R                  D6$ )r  rx   Tr+   )r{   r%   r7   r3   r  _next_is_stickyr   r  )r$   rB   s     r'   r(  Rcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._apply_rel_filtersL  s]     7xx#>>$((3*.H'++-44It7H7HIIr*   c                    > [        TR                  R                  U R                  5       H  u  p UR                  U R
                     s  $    g ! [        [        4 a     M7  f = fr   )rT   r   rO   r%   r+  r  rd   r   r$   r%   rH   r   s      r'   get_prefetch_cacheRcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_cacheV  s_      3399??DMMR#==d>V>VWW  S
  '1 s   AA$#A$c                    > [        TR                  R                  U R                  5       H+  u  p UR                  R                  U R                  5        M-     g ! [        [        4 a     MC  f = fr   )	rT   r   rO   r%   r+  r,  r  rd   r   r  s      r'   r-  Zcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._remove_prefetched_objects`  s]      3399??DMMR66::4;S;ST  S '1 s   %AA0/A0c                 l   > U R                  5       =nb  U$ [        TU ]	  5       nU R                  U5      $ r   )r  r   rs   r(  )r$   cacherB   r   s      r'   rs   Lcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_queryseti  s:    0022? 7/1..x88r*   c                   >^^ U(       a  [        U5      S:w  a  [        S5      eU(       a  US   O[        T	U ]  5       nUR	                  US   S9  UR                  UR                  =(       d    U R                  5      n[        X0R                  U5      nU R                  R                  R                  U R                  5      mTR                  R                  R                  n[        UR                      mTR"                  R$                  nUR'                  TR(                   Vs0 sH0  nSUR*                  -  U" U5      < SU" UR,                  5      < 3_M2     snS9nUUU4S jUU4S	 jS
U R.                  S
4$ s  snf )Nrv   rw   r   rx   z_prefetch_related_val_%sr`   )selectc                 F   >^  [        UU 4S jTR                   5       5      $ )Nc              3   t   >#    U H.  nUR                  [        TS UR                   35      T5      v   M0     g7f)_prefetch_related_val_Nget_db_prep_valuer   r    )r   f
connectionresults     r'   r   {create_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_querysets.<locals>.<lambda>.<locals>.<genexpr>  sE      %
 5	 ''*@(LM"  5s   58)r   r   )r  r  fks   `r'   <lambda>hcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_querysets.<locals>.<lambda>  s    u %
  44%  r*   c                 F   >^  [        UU 4S jTR                   5       5      $ )Nc              3   n   >#    U H+  nUR                  [        TUR                  5      T5      v   M-     g 7fr   r  )r   r  r  r   s     r'   r   r    s3      #6 ''aii(@*MM6s   25)r   r~   )r   r  r  s   `r'   r  r    s    U #66# r*   F)ry   rz   r   rs   r{   r3   r7   rJ   r  r  rL   r  r  rO   r   r   rF   ops
quote_nameextrar   r    columnr  )
r$   rD   r   rB   
join_tableqnr  r  r  r   s
          @@r'   r   Vcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_querysetsp  sa   S^q0 #  (1y|eg6J6LH16~~hll&>dhh?H0//H ##--d.D.DEB00J$X[[1J**B~~
  44	 5 /ii *~r!((|"55 5	 & H  (( s   &6E=c                    U R                   R                  (       d  g [        R                  " U R                  U R
                  S9n[        U   R                  R                  (       d  g SU R
                  0nU R                  R                  R                  XS9nU R                  U R                  S   0nU R                   R                  (       a  SUSU R                  -  '   UR                  " S0 UD6$ )Nrx   r%   rm   r   Fz
%s__isnullr+   )r!  db_constraintr   r  r  r%   r   r:   supports_foreign_keysro   rp   r  r  r   r  r   )r$   rF   rn   r  r  s        r'   constrained_targetRcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.constrained_target  s    
 $$22##DLL4==IBr?++AA/Ell00;;B;LG--t/?/?/BCG   %%AFt'='==>>>,G,,r*   c                    > T[         L a0  U R                  5       c  U R                  =nb  UR                  5       $ [        TU ]  5       $ r   )r
   r  r  existsr   r$   r  r   r  s     r'   r  Fcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.exists  sH    g%++-5+/+B+BB'O)0022w~''r*   c                    > T[         L a0  U R                  5       c  U R                  =nb  UR                  5       $ [        TU ]  5       $ r   )r
   r  r  countr   r  s     r'   r  Ecreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.count  sG    g%++-5+/+B+BB'O)//11w}&r*   through_defaultsc                   U R                  5         [        R                  " U R                  U R                  S9n[
        R                  " USS9   U R                  " U R                  U R                  /UQ7SU06  U R                  (       a+  U R                  " U R                  U R                  /UQ7SU06  S S S 5        g ! , (       d  f       g = f)Nrx   Fr=  r  )r-  r   r   r  r%   r	   rA  
_add_itemsr  r  r  )r$   r  rC  rF   s       r'   rE  Ccreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.add  s    ++-$$T\\DMMJB##">****  &6	 ##OO....  *:	 ?>>s   A(C  
Cc                T   #    [        U R                  5      " USU06I S h  vN $  N7f)Nr  rI  )r$   r  rC  s      r'   rK  Dcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aadd  s0     &txx0(8   rM  c                 t    U R                  5         U R                  " U R                  U R                  /UQ76   g r   )r-  _remove_itemsr  r  r$   rC  s     r'   rl  Fcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.remove  s0    ++-t55t7M7MUPTUr*   c                 N   #    [        U R                  5      " U6 I S h  vN $  N7fr   ro  r  s     r'   rp  Gcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aremove  s     &t{{3T::::s   %#%c                   > [         R                  " U R                  U R                  S9n[        R
                  " USS9   [        R                  R                  U R                  SU R                  U R                  U R                  S US9  U R                  5         U R                  [        TU ]9  5       R                  U5      5      nU R                  R                   R                  U5      R#                  U5      R%                  5         [        R                  R                  U R                  SU R                  U R                  U R                  S US9  S S S 5        g ! , (       d  f       g = f)Nrx   Fr=  	pre_clearsenderactionr%   r  rO   pk_setr3   
post_clear)r   r   r  r%   r	   rA  r   m2m_changedsendr  rO   r-  r  r   rs   r3   r   r   delete)r$   rF   r  r   s      r'   rt  Ecreate_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
E,c                 R   #    [        U R                  5      " 5       I S h  vN $  N7fr   rw  rf   s    r'   rx  Fcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aclear  s     &tzz24444r  F)rt  r  c                   [        U5      n[        R                  " U R                  U R                  S9n[
        R                  " USS9   U(       a#  U R                  5         U R                  " USU06  O[        U R                  U5      R                  U R                  R                  R                  SS95      n/ nU H  n[        XpR                  5      (       a  U R                  R!                  U5      S   OU R                  R#                  U5      nX;   a  UR%                  U5        Mn  UR'                  U5        M     U R$                  " U6   U R                  " USU06  S S S 5        g ! , (       d  f       g = f)Nrx   Fr=  r  Tflatr   )r   r   r   r  r%   r	   rA  rt  rE  ri  r3   values_listr!  r    r   rO   r|   get_prep_valuerl  r?  )	r$   rC  rt  r  rF   rk  r  r   fk_vals	            r'   ri  Ccreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.set  s3    ;D$$T\\DMMJB##">JJLHHdF5EF!

222 --::BB 3 G  "H#  *#zz:: !--GGLQO!%!2!2!A!A#!F 
 ",#NN62$OOC0  $ KK)HHhJ9IJ1 ?>>s   
DE,,
E:c                R   #    [        U R                  5      " XUS9I S h  vN $  N7f)N)rC  rt  r  r  )r$   rC  rt  r  s       r'   r  Dcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aset4  s+     &txx09I   r  c                   > [         R                  " U R                  R                  U R                  S9n[        TU R                  U5      ]  " S0 UD6nU R                  XAS9  U$ Nrx   r  r+   )r   r   r%   r   r   rp   rP  rE  )r$   r  r   rF   new_objManyRelatedManagerr   s        r'   rP  Fcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.create;  sT    $$T]]%<%<t}}UB.0CKUfUGHHWH@Nr*   c                X   #    [        U R                  5      " SSU0UD6I S h  vN $  N7fNr  r+   rV  r$   r  r   s      r'   rX  Gcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.acreateC  s5     &t{{3 !15;      !*(*c                   > [         R                  " U R                  R                  U R                  S9n[        TU R                  U5      ]  " S0 UD6u  pEU(       a  U R                  XAS9  XE4$ r
  )r   r   r%   r   r   rp   r[  rE  r$   r  r   rF   r   createdr  r   s         r'   r[  Mcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_or_createJ  sc    $$T]]%<%<t}}UB !3T__R5HW LC
 @<r*   c                X   #    [        U R                  5      " SSU0UD6I S h  vN $  N7fr  r^  r  s      r'   r_  Ncreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aget_or_createW  s7     &t'9'9: !15;   r  c                   > [         R                  " U R                  R                  U R                  S9n[        TU R                  U5      ]   " S0 UD6u  pEU(       a  U R                  XAS9  XE4$ r
  )r   r   r%   r   r   rp   rb  rE  r  s         r'   rb  Pcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.update_or_create^  sm    $$T]]%<%<t}}UB "DOOB$7 )!')LC
 @<r*   c                X   #    [        U R                  5      " SSU0UD6I S h  vN $  N7fr  re  r  s      r'   rf  Qcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aupdate_or_createk  s7     &t'<'<= !15;   r  c           
         SSK Jn  [        5       nU R                  R                  R                  U5      nU GH,  n[        X`R                  5      (       a  [        R                  " X`R                  5      (       dJ  [        SU< SU R                  R                  R                  < SUR                  R                  < S35      eUR                  U5      S   nUc  [        SU< SU< S35      eUR                  U5        M  [        Xc5      (       a1  [!        S	U R                  R                  R"                  < S
U< 35      eUR                  UR%                  U5      5        GM/     U$ )zC
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 Noner7  r8  )django.db.modelsr  ri  r  rL   r  r   rO   r   r   r%   rz   r   rF   r|   rE  r  r   r  )r$   r  rC  r  
target_idsr!  r   	target_ids           r'   _get_target_idsOcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._get_target_idsr  s!    /J<<--778IJLc::..!00mmDD(  #DMM$8$8$;$;SZZ]]L 
 !- F Fs KA NI (("$57  NN9-++#::++77> 
 NN<#>#>s#CD+ , r*   c                     U R                   R                  R                  U5      R                  USS9R                  " S0 XR
                  S   SU-  U0D6nUR                  U5      $ )zW
Return the subset of ids of `objs` that aren't already assigned to
this relationship.
Tr  r   r   r+   )r  r   r3   r  r   r  
difference)r$   r  r  rF   r   valss         r'   _get_missing_target_idsWcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._get_missing_target_ids  sw     --33B7.T:  *+;+;A+> #44j  ((..r*   c                 V   U R                   R                  R                  SL=(       a    [        U   R                  R
                  nU R                  =(       d    X R                  :H  =(       a)    [        R                  R                  U R                   5      nUUU=(       a    U(       + 4$ )a/  
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  rL   auto_createdr   r:   supports_ignore_conflictsr  r  r   r  has_listeners)r$   rF   r  can_ignore_conflictsmust_send_signalss        r'   _get_add_planMcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._get_add_plan  s     ""//u< GO,,FF ! K 15K5K K!D&&44T\\B  %!%?.?*? r*   c                   U(       d  g [        [        U=(       d    0 5      5      nU R                  X$5      n[        R                  " U R
                  U R                  S9nU R                  Xa5      u  pxn	U	(       ak  U R
                  R                  R                  U5      R                  U V
s/ sH,  n
U R
                  " S0 SU-  U R                  S   SU-  U
0D6PM.     sn
SS9  g U R                  XXe5      n[        R                  " USS9   U(       aK  [        R                   R#                  U R
                  SU R                  U R$                  U R&                  UUS	9  U R
                  R                  R                  U5      R                  U V
s/ sH.  n
U R
                  " S0 UDSU-  U R                  S   SU-  U
0D6PM0     sn
US9  U(       aK  [        R                   R#                  U R
                  S
U R                  U R$                  U R&                  UUS	9  S S S 5        g s  sn
f s  sn
f ! , (       d  f       g = f)Nrx   z%s_idr   T)ignore_conflictsFr=  pre_addr  post_addr+   )dictr   r"  r   r   r  r%   r/  r   r3   bulk_creater  r'  r	   rA  r   r  r  r  rO   )r$   r  r  r  rC  r   rF   r-  r.  can_fast_addr!  missing_target_idss               r'   r  Jcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._add_items  sI    #$56F6L"$MN--.?FJ$$T\\DMMJBDHDVDVEA \ --33B7CC *4 *4I   '*; ;T=M=Ma=P '*; ;Y *4 &* D  !%!=!=!b" ##">$'',,#||(!% $"jj1  -  --33B7CC *<	 *<I  . !(*; ;T=M=Ma=P '*; ;Y *<	 &: D  %'',,#||)!% $"jj1  - 7 ?>!:	 ?>s&   -2H'BH14H,
AH1,H11
H?c                 J  > U(       d  g [        5       nU H_  n[        XPR                  5      (       a1  U R                  R	                  U5      S   nUR                  U5        MN  UR                  U5        Ma     [        R                  " U R                  U R                  S9n[        R                  " USS9   [        R                  R                  U R                  SU R                  U R                  U R                  UUS9  [         TU ]E  5       nUR%                  5       (       aE  UR'                  U5      R(                  " S	0 SU R                  R                  R*                  -  U0D6n	OUn	U R-                  U	5      n
U R                  R.                  R'                  U5      R)                  U
5      R1                  5         [        R                  R                  U R                  SU R                  U R                  U R                  UUS9  S S S 5        g ! , (       d  f       g = f)
Nr   rx   Fr=  
pre_remover  r   post_remover+   )ri  r   rO   r!  r|   rE  r   r   r  r%   r	   rA  r   r  r  r  r   rs   r  r3   r   r    r  r   r  )r$   r  r  rC  rk  r   r  rF   target_model_qsold_valsr  r   s              r'   r  Mcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._remove_items  s   
  eGc::..!..HHMaPFKK'KK$  $$T\\DMMJB##">##((<<'!]] LL**" )  #('"6"8"//1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   7EH
H")r  r%   rO   r  r  r  r  r  r  r  r  r!  r  r  r   )'r,   r-   r.   r/   r[   r  r  r  r(  r  r-  rs   r   r  r  r  r  rE  r  rK  rl  rp  rt  rx  ri  r  rP  rX  r[  r_  rb  rf  r"  r'  r/  r  r  r0   r   )r   r  r   r  r  s   @r'   r  r    s   0	 0	d	9 $( 	(	J			90	d 
	- 
	-$	(	' /3 	( 59 	
  	V "	; #	4 !	5 "%*T 	K@ ,1D 	
  -1 	 	 "48 	
 #48 		  		  %)!;? 	
 &*"7; 		  		  (,$>B 	
 )-%	@	/&	D QUE	N/	 /	r*   r  r  )r  r   r  r  s   ```@r'   r  r    s"    @	 @	Z @	D r*   N),r   asgiref.syncr   django.core.exceptionsr   	django.dbr   r   r   r   r	   r  r
   r   r   r   django.db.models.expressionsr   %django.db.models.fields.tuple_lookupsr   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   rJ   rT   rV   r   r   r   r   r  r  r+   r*   r'   <module>rK     s   ?B ' -  9 8 1 9 0 A + : @ 36"3 60	$_< _<D1@ : 1@h@@ @@F6
 6
rl^	,
5 ,
^J	r*   