
    jo                    V   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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prefetch_related_objects)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      c/var/www/html/KUNJ/venv/lib/python3.13/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_r7   windows              r(   _filter_prefetch_querysetrL   `   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_ancestorsrV   x   sb     (

&(..@@G"D(( 112BDI((# 
&s   AAAc                   l    \ 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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_fieldrQ   DoesNotExistAttributeErrorr.   r/   namer%   s    r(   ra   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	                  SU0S9R                  UR                  R
                  5      $ Nr&   hints)r    rd   rQ   _base_manager
db_manager
fetch_mode_staterk   s     r(   get_queryset'ForwardManyToOneDescriptor.get_queryset   sM    zz&&,,::EEx( F 

*X__//
0	1r+   Nc           
         SnU(       a   [        U5      S:w  a  [        S5      eUS   nO"SnU R                  US   S9R                  5       nU R                  R
                  nU R                  R                  nU Vs0 s H  ov" 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(       a  UR+                  5         U	R,                  (       d$  U H  nX" U5         nU	R/                  X5        M      UUUSU R                  R0                  S4$ s  snf s  sn
f NF   Iquerysets argument of get_prefetch_querysets() should have a length of 1.r   Tr&   )len
ValueErrorru   _disable_cloningr    get_foreign_related_valueget_local_related_valuerd   foreign_related_fieldsr:   resolve_refrg   targetfilterr   r   rQ   rN   db_tablelistclear_ordering_enable_cloningmultipleset_cached_value
cache_name)r%   rF   	querysets_cloning_disabledrD   rel_obj_attrinstance_attrinstinstances_dictrd   r    related_fieldsrel_objr&   s                 r(   get_prefetch_querysets1ForwardManyToOneDescriptor.get_prefetch_querysets   s   !9~" #  !|H $(()A,(?PPRHzz;;

::@IJ	--t3	Jzz.. ::
: NN&&uzz299: 	 
 ??NN((11""JJ	 ^$

 	%%'$$& $$#),w*?@--g@ $ JJ!!
 	
= K
s   <G>9Gc                 t    U R                  US9nUR                  U R                  R                  U5      5      $ Nr{   )ru   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cI  U(       aB  UR                  R                  R                  X5        U R                   R                  U5      s $ U R                   R                  X5         GNXf = 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 rb   )r    rP   KeyErrorr   rQ   rV   rt   rs   fetchr   nullra   r-   rg   )r%   r&   clsr   	has_valuerQ   rS   rU   s           r(   __get__"ForwardManyToOneDescriptor.__get__   s:    K
	;jj11(;G* ?4::??00#'::#3#3#<#<djjooN  N3  	;GDJJ$F$Fx$PPI

((2Ee2V.$x #'**"="=hPT"="U".! 3W 9**00@zz228<<JJ'':%	;s%   B AE;-E;AE;E;:E;c                     U R                  U5      nU R                  R                  X5        U R                  R                  nUR                  (       d  UR                  X!5        g g r   )r   r    r   rd   r   )r%   r&   r   rd   s       r(   	fetch_one$ForwardManyToOneDescriptor.fetch_one  sO    //(+

##H6 zz..$$))'< %r+   c                     U R                   nU Vs/ s H  o2" U5      (       a  M  UPM     nn[        X@R                  R                  5        g s  snf r   )r"   r   r    rg   r%   rF   r"   imissing_instancess        r(   
fetch_many%ForwardManyToOneDescriptor.fetch_many   s=    NN	(1F	11Q	F !2JJOOD Gs
   AA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 "": "rb   " must be a "" instance.r{   GCannot assign "%r": the current database router prevents this relation.r   )
isinstancer    rd   rQ   rN   concrete_modelr}   object_namerg   rt   rH   r   db_for_write	__class__allow_relationrP   r   r   setattrr!   getattrr   )r%   r&   r'   rd   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    rQ   rg   rh   s    r(   
__reduce__%ForwardManyToOneDescriptor.__reduce__q  s&     ))4::??;;;r+   r[   r   )r-   r.   r/   r0   __doc__r]   r   ra   r"   ru   r   r   r   r   r   r)   r   r1   r,   r+   r(   rX   rX      sS    
$ 
 
".1
4
lG
+Z=E
J;X<r+   rX   c                   8   ^  \ rS rSrSrU 4S jrU 4S jrSrU =r$ )ForwardOneToOneDescriptoriz  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                   >^ U R                   R                  R                  (       G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      (       d  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R                  R                  UR                  l        U$ [        TU ]=  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    rd   parent_linkget_deferred_fieldsrQ   rN   concrete_fieldsr!   anyr   rt   addingrH   rs   superr   )	r%   r&   deferred	rel_modelr    kwargsobjr   r   s	          @r(   r   $ForwardOneToOneDescriptor.get_object  s    ::""...335H 

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

! ( 2 2

(0(B(B

%
w!(++ R Os   1E'E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_keyrd   r   rN   r   rQ   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/   r0   r   r   r)   r1   __classcell__r   s   @r(   r   r   z  s    
,(@ @r+   r   c                   v    \ 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
 5       rS rS 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`   )	rc   r   related_modelre   rf   rQ   r.   r/   rg   rh   s    r(   ra   3ReverseOneToOneDescriptor.RelatedObjectDoesNotExist  sk     '\\''44nE"ll00;; LL&&33LL%%!
 	
r+   c                 8    U R                   R                  U5      $ r   )r   r"   rk   s     r(   r"   #ReverseOneToOneDescriptor.is_cached  s    ||%%h//r+   c                    U R                   R                  R                  R                  SU0S9R	                  UR
                  R                  5      $ rn   )r   r   rq   rr   rs   rt   rk   s     r(   ru   &ReverseOneToOneDescriptor.get_queryset  sG    ||))77BBx( C 

*X__//
0	1r+   Nc                    SnU(       a   [        U5      S:w  a  [        S5      eUS   nO"SnU R                  US   S9R                  5       nU R                  R
                  R                  nU R                  R
                  R                  nU Vs0 s H  ov" 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(       a  UR                  5         U H2  n
X" U
5         nU R                  R
                  R                  X5        M4     UUUSU R                  R                  S4$ s  snf )	NFry   rz   r   Tr{   %s__inr,   )r|   r}   ru   r~   r   r    r   r   rg   r   r:   r   r   r   r   )r%   rF   r   r   rD   r   r   r   r   r:   r   r&   s               r(   r   0ReverseOneToOneDescriptor.get_prefetch_querysets  sT   !9~" #  !|H $(()A,(?PPRH||))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$ ! [         av    UR                  5       (       d  SnO@UR                  R
                  R                  X5        U R                   R                  U5      nU R                   R                  X5         Nf = 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``.
Nr   rb   )r   rP   r   
_is_pk_setrt   rs   r   r   ra   r   r-   accessor_name)r%   r&   r   r   s       r(   r   !ReverseOneToOneDescriptor.__get__  s     K
	=ll33H=G ?00%%..0J0JL 
 N  	=&&((**00@,,77ALL))(<	=s   A( (A=C('C(c                     U R                   $ )z
Add compatibility with the fetcher protocol. While self.related is not
a field but a OneToOneRel, it quacks enough like a field to work.
r   rh   s    r(   r    ReverseOneToOneDescriptor.field%  s     ||r+   c                 f   U R                   R                  R                  U5      n U R                  US9R                  " S0 UD6nU R                   R                  R                  X15        U R                   R                  X5        g ! U R                   R                  R                   a    S n NDf = fNr{   r,   )r   r    get_forward_related_filterru   r   r   r   re   )r%   r&   filter_argsr   s       r(   r   #ReverseOneToOneDescriptor.fetch_one-  s     ll((CCHM	C''':>>MMG LL//B%%h8	 ||))66 	G	s   B &B0/B0c                     U R                   nU Vs/ s H  o2" U5      (       a  M  UPM     nn[        UU R                  R                  5       5        g s  snf r   )r"   r   r   get_accessor_namer   s        r(   r   $ReverseOneToOneDescriptor.fetch_many9  sG    NN	(1F	11Q	F LL**,	
 Gs
   AA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   rb   r   r   r{   r   c              3   P   >#    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>x  s&      FE %--00Fs   #&)r   rP   r#   r   r    rg   r   r   r}   rN   r   r   rt   rH   r   r   r   r   tupler   	enumeratelocal_related_fieldsr!   r   )r%   r&   r'   r   
related_pkindexr    s    `     r(   r)   !ReverseOneToOneDescriptor.__set__A  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   rQ   rg   rh   s    r(   r   $ReverseOneToOneDescriptor.__reduce__  s&    ++T\\->->???r+   r   r   )r-   r.   r/   r0   r   r]   r   ra   r"   ru   r   r   propertyr    r   r   r)   r   r1   r,   r+   r(   r   r     sb    

 
 
 01
%
N"H  
9
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__  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_cls  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__  s      K''11r+   c                 2    SU R                   R                  4$ )Nzreverse side of a related set)r  r   rh   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/   r0   r   r]   r   r  r   r  r)   r1   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   rQ   r    rg   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   rQ   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!   r}   )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                  5          UR                  U R                  S9  U R                   (       a  UR                  U R                   5      nU R                  R                  R                  Ul        SUl        UR                  " S0 U R                   D6nSSS5        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$ ! , (       d  f       N= f! [0         ad    [3        U R"                  R4                  S   R6                   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.
r{   TN r,   )r9   r   db_for_readrQ   r&   r   r<   !interprets_empty_strings_as_nulls_avoid_cloning
_add_hintsr5   rt   rs   _fetch_mode_defer_next_filterr   r#  r    r   r   r!   nonemany_to_onetarget_fieldr   r  
path_infostarget_fields_known_related_objects)r%   rD   rH   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 " ((*##T]]#;88'~~dhh7H'+}}';';'F'F$.2+#???T->->? + ::dmmU]];;3"91F1F#==?* ; zz%%
N#'::#:#:L ")8L8L!MJJJ]] ;3/ OA +*$ "  "' 15

0E0Eb0I0W0W0W $DMM<3G3GH0W"Js+   *BG=G$ 
G!$4I*I
IIc                      U R                   R                  R                  U R                  R                  R
                  5        g ! [        [        4 a     g f = fr   )r&   _prefetched_objects_cachepopr    rd   r   rf   r   rh   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   r}   r   r-   rE  r    rd   r   rf   r   r   ru   rB  )r%   rD   r   s     r(   ru   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                    > SnU(       a   [        U5      S:w  a  [        S5      eUS   nOSn[        TU ]  5       R	                  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  ov" U5      U_M     nn[        X@R                  R                  U5      nU(       a  UR                  5         U HJ  n	U R                  R                  U	5      (       a  M%  X" U	5         n
U R                  R                  X5        ML     U R                  R                   R"                  nXEUSUS4$ s  snf rx   )r|   r}   r   ru   r~   r6  r5   r9   r    r   r   rL   rg   r   r"   r   rd   r   )r%   rF   r   r   rD   r   r   r   r   r   r&   r   r   s               r(   r   Qcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_prefetch_querysets,  sP    %y>Q&$'  %Q<$(! 7/1BBD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   9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)Nr{   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   rQ   r  rN   r   r   r    rg   r&   )r   r%   s    r(   check_and_update_obj\create_reverse_many_to_one_manager.<locals>.RelatedManager.add.<locals>.check_and_update_objR  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r5   	savepointr,   )r-  rG  r   r   rQ   r&   rt   r   rH   r}   appendr   rq   r5   r   updater    rg   r	   atomicsave)r%   rN  objsrH   rS  pksr   s   `      r(   add>create_reverse_many_to_one_manager.<locals>.RelatedManager.addM  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NrN  r   r_  r%   rN  r]  s      r(   aadd?create_reverse_many_to_one_manager.<locals>.RelatedManager.aaddt  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$ r   )r-  rG  r&   r    rg   r   r   rQ   r   rr   creater%   r   rH   RelatedManagerr   s      r(   ri  Acreate_reverse_many_to_one_manager.<locals>.RelatedManager.createy  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   ri  r%   r   s     r(   acreateBcreate_reverse_many_to_one_manager.<locals>.RelatedManager.acreate  s"     &t{{3=f====rg  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$ r   )
r-  r&   r    rg   r   r   rQ   r   rr   get_or_createrj  s      r(   rt  Hcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_or_create  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rn  r   rt  rp  s     r(   aget_or_createIcreate_reverse_many_to_one_manager.<locals>.RelatedManager.aget_or_create  s$     &t'9'9:DVDDDDrg  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$ r   )
r-  r&   r    rg   r   r   rQ   r   rr   update_or_createrj  s      r(   r{  Kcreate_reverse_many_to_one_manager.<locals>.RelatedManager.update_or_create  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rn  r   r{  rp  s     r(   aupdate_or_createLcreate_reverse_many_to_one_manager.<locals>.RelatedManager.aupdate_or_create  s$     &t'<'<=GGGGGrg  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 )NrQ  rR  z is not related to rb   rU  )r-  r    r   r&   setr   rQ   r  rN   r   r   r_  r   rd   re   _clearr   )r%   rN  r]  r@  old_idsr   s         r(   removeAcreate_reverse_many_to_one_manager.<locals>.RelatedManager.remove  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rb  r   r  rd  s      r(   aremoveBcreate_reverse_many_to_one_manager.<locals>.RelatedManager.aremove  s$     *4;;7IDIIIIrg  c                F    U R                  5         U R                  X5        g r   )r-  r  r%   rN  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)NrM  r   r  r  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)Nr{   FrW  )update_fieldsr,   )rG  r   r   rQ   r&   r5   rZ  r    rg   r	   r[  r   r\  )r%   rD   rN  rH   r   s        r(   r  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)rN  r  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)Nr{   FrW  rM  rN  )r-  r  r    r   r   r   rQ   r&   r	   r[  r  r_  r  r5   allr  rY  )r%   r]  rN  r  rH   old_objsnew_objsr   s           r(   r  >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)r]  rN  r  r   r  )r%   r]  rN  r  s       r(   aset?create_reverse_many_to_one_manager.<locals>.RelatedManager.aset  s!     &txx0dUSSSS   '%')r#  r    r&   rQ   r   ) r-   r.   r/   r0   r]   r(  do_not_call_in_templatesr-  rB  rG  ru   r   r_  alters_datare  ri  rq  rt  rx  r{  r  r    r   r  r  r  r  r  r  r  r1   r   )r   rk  r  s   @r(   rk  r!    sK   	<	0
 $( 	(	T		9"	SB #' #	#J )- 	C  	O "	> #	V %)!	E &*"	Y (,$	H )-% 99>>)- ?0 "&F04 J #'G$( ( !%E+/ B "&FF "&F$( 	+4 +/u 	T  r+   rk  r   )
superclassr  rk  s    `@r(   r  r    s"    m  m Z m ^	 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  throughrh   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   rQ   #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    rg   rh   s    r(   r  4ManyToManyDescriptor._get_set_deprecation_msg_params)  sI    + LLyi9&*llDHH""
 	
 9=


 	
r+   r  )F)r-   r.   r/   r0   r   r]   r  r  r   r  r  r1   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                     >^  \ 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S.S jrSS.S jrS\l        SS.S jrS\l        SSS.S jrS rS\l        S rS\l        S.U 4S jjrS rS\l        S rS\l        SSSS.S jr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S.S* jr'SSS.U 4S+ jjr(S,r)U =r*$ )/?create_forward_many_to_many_manager.<locals>.ManyRelatedManageri9  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&   rQ   r    related_query_namequery_field_namerg   prefetch_cache_namem2m_field_namesource_field_namem2m_reverse_field_nametarget_field_namesymmetricalr   r  r  rN   	get_fieldsource_fieldr;  r#  pk_field_namesr   r   r!   r   related_valr}   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   rQ   r  r   r&   )r%   r&  r'  r  r  s      r(   r(  Hcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.__call__l  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$ )Nr3   )	r   ri  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_filtersu  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                  5          UR                  U R                  S9  U R                  (       a  UR	                  U R                  5      nU R                  R
                  R                  Ul        SUl        UR                  5       R                  " S0 U R                  D6sSSS5        $ ! , (       d  f       g= f)r0  r{   TNr,   )r5  r6  r&   r9   r5   rt   rs   r7  r8  _next_is_stickyr   r#  )r%   rD   s     r(   rB  Rcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._apply_rel_filters  s     ((*##T]]#;88'~~dhh7H'+}}';';'F'F$.2+//188M4;L;LM +**s   BB66
Cc                    > [        TR                  R                  U R                  5       H  u  p UR                  U R
                     s  $    g ! [        [        4 a     M7  f = fr   )rV   r    rQ   r&   rE  r  rf   r   r%   r&   rJ   r  s      r(   get_prefetch_cacheRcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_cache  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   )	rV   r    rQ   r&   rE  rF  r  rf   r   r  s      r(   rG  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   ru   rB  )r%   cacherD   r   s      r(   ru   Lcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_queryset  s:    0022? 7/1..x88r+   c                 L  >^^	 SnU(       a   [        U5      S:w  a  [        S5      eUS   nOSn[        T
U ]  5       R	                  5       nUR                  US   S9  UR                  UR                  =(       d    U R                  5      n[        X@R                  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(       a  UR1                  5         UUU	4S
 jUU	4S jSU R2                  S4$ s  snf )NFry   rz   r   Tr{   z_prefetch_related_val_%srb   )selectc                 F   >^  [        UU 4S jTR                   5       5      $ )Nc              3   v   >#    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   69)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   p   >#    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   36)r  r   )r   r  r  s   `r(   r  r    s    U #66# r+   )r|   r}   r   ru   r~   r6  r5   r9   rL   r  r  rN   r  r  rQ   r   r   rH   ops
quote_nameextrar  r!   columnr   r  )r%   rF   r   r   rD   
join_tableqnr  r  r  r   s           @@r(   r   Vcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_querysets  s    %y>Q&$'  %Q<$(! 7/1BBD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   27F!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$ )Nr{   r&   ro   r   Fz
%s__isnullr,   )r;  db_constraintr   r3  r  r&   r   r<   supports_foreign_keysrq   rr   r  r  r   r  r   )r%   rH   rp   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+   Fthrough_defaultsr5   rawc                   U=(       d)    [         R                  " U R                  U R                  S9n[        R
                  " USS9   U R                  " U R                  U R                  /UQ7UUUS.6  U R                  (       a-  U R                  " U R                  U R                  /UQ7UUUS.6  S S S 5        g ! , (       d  f       g = f)Nr{   FrW  r  )
r   r   r  r&   r	   r[  
_add_itemsr  r  r  )r%   r   r5   r  r]  rH   s         r(   	_add_baseIcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._add_base  s    S&--dllT]]SB##">****  &6 ##OO....  *:  ?>>s   A,B==
Cr   c                    U R                  5         [        R                  " U R                  U R                  S9nU R
                  " X!US.6  g )Nr{   )r   r5   )rG  r   r   r  r&   r  )r%   r   r]  rH   s       r(   r_  Ccreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.add)  s6    ++-$$T\\DMMJBNND2Nr+   c                T   #    [        U R                  5      " USU06I S h  vN $  N7f)Nr   rc  )r%   r   r]  s      r(   re  Dcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aadd0  s0     &txx0(8   rg  r5   r  c                    U=(       d)    [         R                  " U R                  U R                  S9nU R                  " U R
                  U R                  /UQ7XBS.6  g )Nr{   r  )r   r   r  r&   _remove_itemsr  r  )r%   r5   r  r]  rH   s        r(   _remove_baseLcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._remove_base7  sP    S&--dllT]]SB&&(>(>AEMOr+   c                     U R                  5         [        R                  " U R                  U R                  S9nU R
                  " USU06  g )Nr{   r5   )rG  r   r   r  r&   r  )r%   r]  rH   s      r(   r  Fcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.remove=  s;    ++-$$T\\DMMJBt.2.r+   c                 N   #    [        U R                  5      " U6 I S h  vN $  N7fr   r  )r%   r]  s     r(   r  Gcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aremoveD  s     &t{{3T::::s   %#%c                   > U=(       d)    [         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US9  U R                  [        TU ]5  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US9  S S S 5        g ! , (       d  f       g = f)Nr{   FrW  	pre_clearsenderactionr&   r  rQ   pk_setr5   r  
post_clear)r   r   r  r&   r	   r[  r   m2m_changedsendr  rQ   r  r   ru   r5   r  r   delete)r%   r5   r  rH   r  r   s        r(   _clear_baseKcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._clear_baseI  s   S&--dllT]]SB##">##((<<&!]] LL** ) 	 44UW5I5K5Q5QRT5UV--33B7>>wGNNP##((<<'!]] LL** ) 	 ?>>s   	DE
E'c                     U R                  5         [        R                  " U R                  U R                  S9nU R                  US9  g )Nr{   r4   )rG  r   r   r  r&   r  )r%   rH   s     r(   r  Ecreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.cleard  s9    ++-$$T\\DMMJB2&r+   c                 R   #    [        U R                  5      " 5       I S h  vN $  N7fr   r  rh   s    r(   r  Fcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.acleark  s     &tzz24444r  )r  r   r  c                   [        U5      n[        R                  " U R                  U R                  S9n[
        R                  " USS9   U R                  5         U(       a"  U R                  XTS9  U R                  " XXTS.6  O[        U R                  U5      R                  U R                  R                  R                  SS95      n/ nU H  n[        XR                   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*                  " XeUS.6  U R                  " XsXTS.6  S S S 5        g ! , (       d  f       g = f)	Nr{   FrW  r  r  Tflatr   )r  r   r   r  r&   r	   r[  rG  r  r  r  r5   values_listr;  r!   r   rQ   r   get_prep_valuer  rY  r  )
r%   r]  r  r   r  rH   r  r  r   fk_vals
             r(   set_baseHcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.set_basep  sK    ;D$$T\\DMMJB##">//1$$2$7NN "

222 --::BB 3 G  "H#  *#zz:: !--GGLQO!%!2!2!A!A#!F 
 ",#NN62$OOC0  $ %%wcBNN!B7 ?>>s   
D+E>>
Fr  r   c                $    U R                  XUS9  g )Nr,  )r*  r%   r]  r  r   s       r(   r  Ccreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.set  s    MM$>NMOr+   c                R   #    [        U R                  5      " XUS9I S h  vN $  N7f)N)r]  r  r   r  r.  s       r(   r  Dcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aset  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$ Nr{   r  r,   )r   r   r&   r   r   rr   ri  r_  )r%   r   r   rH   new_objManyRelatedManagerr   s        r(   ri  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,   ro  r%   r   r   s      r(   rq  Gcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.acreate  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$ r3  )r   r   r&   r   r   rr   rt  r_  r%   r   r   rH   r   createdr5  r   s         r(   rt  Mcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_or_create  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r8  rw  r9  s      r(   rx  Ncreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aget_or_create  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$ r3  )r   r   r&   r   r   rr   r{  r_  r=  s         r(   r{  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r8  r~  r9  s      r(   r  Qcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aupdate_or_create  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 NonerQ  rR  )django.db.modelsrG  r  r  rN   r  r   rQ   r   r   r&   r}   rt   rH   r   r_  r  r   r(  )r%   r  r]  rG  
target_idsr;  r   	target_ids           r(   _get_target_idsOcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._get_target_ids  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  r5   r'  r   r  
difference)r%   r  r  rH   rI  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  rN   auto_createdr   r<   supports_ignore_conflictsr  r  r   r  has_listeners)r%   rH   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U=(       d)    [        R                  " U R
                  U R                  S9nU R                  X5      u  pnU(       al  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X5      n[        R                  " USS9   U
(       aL  [        R                   R#                  U R
                  SU R                  U R$                  U R&                  U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
(       aL  [        R                   R#                  U R
                  S
U R                  U R$                  U R&                  UUUS	9  S S S 5        g s  snf s  snf ! , (       d  f       g = f)Nr{   z%s_idr   T)ignore_conflictsFrW  pre_addr  post_addr,   )dictr   rK  r   r   r  r&   rX  r  r5   bulk_creater  rP  r	   r[  r   r  r  r  rQ   )r%   r  r  r   r5   r  r]  rI  rH   rV  rW  can_fast_addrJ  missing_target_idss                 r(   r  Jcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._add_items,  sT    #$56F6L"$MN--.?FJS&--dllT]]SBDHDVDV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  - 	9 ?>!<	 ?>s&   63H4BH>5H9
AH>9H>>
Ic                `  > 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     U=(       d)    [        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	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	US9  S S S 5        g ! , (       d  f       g = f)
Nr   r{   FrW  
pre_remover  r   post_remover,   )r  r   rQ   r;  r   r_  r   r   r  r&   r	   r[  r   r  r  r  r   ru   r  r5   r   r!   r  r  r  )r%   r  r  r5   r  r]  r  r   r)  rH   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$  S&--dllT]]SB##">##((<<'!]] 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    EH
H-)r#  r&   rQ   r  r  r  r  r  r  r  r  r;  r  r  r   )NF)+r-   r.   r/   r0   r]   r(  r  r  rB  r  rG  ru   r   r  r  r  r  r  r_  r  re  r  r  r  r  r  r  r*  r  r  ri  rq  rt  rx  r{  r  rK  rP  rX  r  r  r1   r   )r   r5  r  r  r  s   @r(   r5  r  9  s   0	 0	d	9 $( 	(
	N			9:	x 
	- 
	-$	(	' 59% 	. /3 	O
 59 	
  ,0e 		/
 "	; #	6	'
 !	5 "*/$E #	J &+T 	P ,1D 	
  -1 	 	 "48 	
 #48 		  		  %)!;? 	
 &*"7; 		  		  (,$>B 	
 )-%	@	/&	L "M	` FJu3	 3	r+   r5  r  )r  r  r  r5  s   ```@r(   r  r  1  s"    u	 u	Z u	n r+   N)-r   asgiref.syncr   django.core.exceptionsr   	django.dbr   r   r   r   r	   rH  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   r   django.db.models.query_utilsr   django.db.models.utilsr   r   django.utils.functionalr   r   rL   rV   rX   r   r   r  r  r  r  r,   r+   r(   <module>rt     s   ?B ' -  9 8 1 9 0 A E : @ 36"3 60	$s< s<l2@ : 2@j]@ ]@@6
 6
rwt	,
5 ,
^	r+   