
    SL,h[                     D   d Z ddlZddlmZ ddlmZ ddlmZmZm	Z	m
Z
mZ ddlmZmZmZ ddlmZ ddlmZmZ dd	lmZ dd
lmZ ddlmZmZ ddlmZ ddlmZ  G d de      Z d Z! G d d      Z" G d de"      Z# G d d      Z$ G d d      Z%d Z& G d de%      Z'd Z(y)a
  
Accessors for related objects.

When a field defines a relation between two models, each model class provides
an attribute to access related instances of the other model class (unless the
reverse accessor has been disabled with related_name='+').

Accessors are implemented as descriptors in order to customize access and
assignment. This module defines the descriptor classes.

Forward accessors follow foreign keys. Reverse accessors trace them back. For
example, with the following models::

    class Parent(Model):
        pass

    class Child(Model):
        parent = ForeignKey(Parent, related_name='children')

 ``child.parent`` is a forward many-to-one relation. ``parent.children`` is a
reverse many-to-one relation.

There are three types of relations (many-to-one, one-to-one, and many-to-many)
and two directions (forward and reverse) for a total of six combinations.

1. Related instance on the forward side of a many-to-one relation:
   ``ForwardManyToOneDescriptor``.

   Uniqueness of foreign key values is irrelevant to accessing the related
   instance, making the many-to-one and one-to-one cases identical as far as
   the descriptor is concerned. The constraint is checked upstream (unicity
   validation in forms) or downstream (unique indexes in the database).

2. Related instance on the forward side of a one-to-one
   relation: ``ForwardOneToOneDescriptor``.

   It avoids querying the database when accessing the parent link field in
   a multi-table inheritance scenario.

3. Related instance on the reverse side of a one-to-one relation:
   ``ReverseOneToOneDescriptor``.

   One-to-one relations are asymmetrical, despite the apparent symmetry of the
   name, because they're implemented in the database with a foreign key from
   one table to another. As a consequence ``ReverseOneToOneDescriptor`` is
   slightly different from ``ForwardManyToOneDescriptor``.

4. Related objects manager for related instances on the reverse side of a
   many-to-one relation: ``ReverseManyToOneDescriptor``.

   Unlike the previous two classes, this one provides access to a collection
   of objects. It returns a manager rather than an instance.

5. Related objects manager for related instances on the forward or reverse
   sides of a many-to-many relation: ``ManyToManyDescriptor``.

   Many-to-many relations are symmetrical. The syntax of Django models
   requires declaring them on one side but that's an implementation detail.
   They could be declared on the other side without any change in behavior.
   Therefore the forward and reverse descriptors can be the same.

   If you're looking for ``ForwardManyToManyDescriptor`` or
   ``ReverseManyToManyDescriptor``, use ``ManyToManyDescriptor`` instead.
    N)sync_to_async)
FieldError)DEFAULT_DB_ALIASNotSupportedErrorconnectionsroutertransaction)QWindowsignals)	RowNumber)GreaterThanLessThanOrEqual)QuerySet)DeferredAttribute)
AltersDataresolve_callables)RemovedInDjango60Warning)cached_propertyc                       e Zd Zd Zy)ForeignKeyDeferredAttributec                    |j                   j                  | j                  j                        |k7  r6| j                  j	                  |      r| j                  j                  |       ||j                   | j                  j                  <   y N)__dict__getfieldattname	is_cacheddelete_cached_valueselfinstancevalues      j/var/www/paragaon_website/venv/lib/python3.12/site-packages/django/db/models/fields/related_descriptors.py__set__z#ForeignKeyDeferredAttribute.__set__Y   sh      !3!34=$**BVBVC
 JJ**8405$**,,-    N)__name__
__module____qualname__r%    r&   r$   r   r   X   s    6r&   r   c                 d   t        di | d|i}| j                  xs t        }| j                  j                  rt
        |   j                  j                  st        d      | j                  j                  | j                  j                  }}| j                  j                  |      j                         D cg c]  \  }}|	 }	}}t        t               ||	      }
|t        |
|      z  }||t!        |
|      z  }| j                  j#                          | j%                  |      S c c}}w )N__inz`Prefetching from a limited queryset is only supported on backends that support window functions.)using)partition_byorder_byr*   )r
   _dbr   query	is_slicedr   featuressupports_over_clauser   low_mark	high_markget_compilerget_order_byr   r   r   r   clear_limitsfilter)queryset
field_name	instances	predicatedbr5   r6   expr_r/   windows              r$   _filter_prefetch_querysetrC   a   s   5
|4()45I		))B~~2''<<#1  'nn55x~~7O7O) ( ; ;" ; E R R T
T1D
 
 	*xP[22	 ;;I##%??9%%
s   <D,c                   \    e Zd ZdZd Zed        Zd Zd ZddZ	ddZ
d	 Zdd
Zd Zd Zy)ForwardManyToOneDescriptoraJ  
    Accessor to the related object on the forward side of a many-to-one or
    one-to-one (via ForwardOneToOneDescriptor subclass) relation.

    In the example::

        class Child(Model):
            parent = ForeignKey(Parent, related_name='children')

    ``Child.parent`` is a ``ForwardManyToOneDescriptor`` instance.
    c                     || _         y r   )r   )r!   field_with_rels     r$   __init__z#ForwardManyToOneDescriptor.__init__   s	    #
r&   c           	      *   t        d| j                  j                  j                  j                  t
        f| j                  j                  j                  | j                  j                  j                  d| j                  j                  dd      S NRelatedObjectDoesNotExist.z.RelatedObjectDoesNotExist)r(   r)   )	typer   remote_fieldmodelDoesNotExistAttributeErrorr(   r)   namer!   s    r$   rK   z4ForwardManyToOneDescriptor.RelatedObjectDoesNotExist   so    
 'ZZ$$**77H"jj..99 JJ$$11JJOO!
 	
r&   c                 8    | j                   j                  |      S r   )r   r   r!   r"   s     r$   r   z$ForwardManyToOneDescriptor.is_cached   s    zz##H--r&   c                     | j                   j                  j                  j                  j	                  |      j                         S N)hints)r   rN   rO   _base_manager
db_managerallr!   rX   s     r$   get_querysetz'ForwardManyToOneDescriptor.get_queryset   s5    zz&&,,::EEEERVVXXr&   Nc                     t        j                  dt        d       || j                  |      S | j                  ||g      S NzLget_prefetch_queryset() is deprecated. Use get_prefetch_querysets() instead.   )
stacklevelwarningswarnr   get_prefetch_querysetsr!   r=   r;   s      r$   get_prefetch_querysetz0ForwardManyToOneDescriptor.get_prefetch_queryset   G    $		
 ..y99**9xjAAr&   c                 >   |rt        |      dk7  rt        d      |r|d   n| j                         }|j                  |d          | j                  j
                  }| j                  j                  }|D ci c]  } ||      | }}| j                  j                  d   }| j                  j                  }	|	j                         s"t        | j                  j                        dk(  r)d|j                  z  |D ch c]  } ||      d    c}i}
nd| j                  j                         z  |i}
 |j                  di |
}|	j                  s$|D ]  }| ||         }|	j                  ||       ! |||d| j                  j                         dfS c c}w c c}w 	N   Iquerysets argument of get_prefetch_querysets() should have a length of 1.r   r"   %s__inTFr*   )len
ValueErrorr]   
_add_hintsr   get_foreign_related_valueget_local_related_valueforeign_related_fieldsrN   	is_hiddenrR   related_query_namer:   multipleset_cached_valueget_cache_name)r!   r=   	querysetsr;   rel_obj_attrinstance_attrinstinstances_dictrelated_fieldrN   r1   rel_objr"   s                r$   re   z1ForwardManyToOneDescriptor.get_prefetch_querysets   s   Y1,  $-9Q<$2C2C2EYq\2zz;;

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

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

 = = ??KE"8??+U+ $$# A),w*?@--gx@A JJ%%'
 	
3 K 'Us   8F=Fc                 z    | j                  |      }|j                  | j                  j                  |            S Nrm   )r]   r   r   get_reverse_related_filter)r!   r"   qss      r$   
get_objectz%ForwardManyToOneDescriptor.get_object   s3    1vvdjj;;HEFFr&   c                    || S 	 | j                   j                  |      }|_| j                   j                  sI| j                  | j                   j                  j                  d| j                   j                  d      |S # t        $ r d| j                   j                  |      v}|r/|j                  j                  | j                   j                        nd}|r@|j                  |      r/|j                  |      }| j                   j                  |d      }nd}|G|rE| j                  |      }| j                   j                  }|j                  s|j                  ||       | j                   j                  ||       Y hw xY w)a=  
        Get the related instance through the forward relation.

        With the example above, when getting ``child.parent``:

        - ``self`` is the descriptor managing the ``parent`` attribute
        - ``instance`` is the ``child`` instance
        - ``cls`` is the ``Child`` class (we don't need it)
        Ndefault has no rL   )r   get_cached_valueKeyErrorrs   _metaget_ancestor_linkrO   r   r   rN   rw   rx   nullrK   r'   rR   )r!   r"   clsr   	has_valueancestor_linkancestorrN   s           r$   __get__z"ForwardManyToOneDescriptor.__get__   s_    K
	;jj11(;G6 ?4::??00#'::#3#3#<#<djjooN  N?  	;DJJ$F$Fx$PPI  001A1AB 
 !8!8!B )99(C **55h5M9//(3#zz66 $,, 11'8DJJ''':1	;s   B DF
	F
c                    |t        || j                  j                  j                  j                  j
                        srt        d|d|j                  j                  d| j                  j                  d| j                  j                  j                  j                  j                  d	      ||j                  j                  0t        j                  |j                  |      |j                  _
        |j                  j                  0t        j                  |j                  |      |j                  _
        t        j                  ||      st        d|z        | j                  j                  }|g| j                  j                  |d	      }||j!                  |d       | j                  j"                  D ]  \  }}t%        ||j&                  d        nI| j                  j"                  D ]0  \  }}t%        ||j&                  t)        ||j&                               2 | j                  j!                  ||       | |j*                  s|j!                  ||       yyy)
aX  
        Set the related instance through the forward relation.

        With the example above, when setting ``child.parent = parent``:

        - ``self`` is the descriptor managing the ``parent`` attribute
        - ``instance`` is the ``child`` instance
        - ``value`` is the ``parent`` instance on the right of the equal sign
        NCannot assign "": "rL   " must be a "" instance.rm   GCannot assign "%r": the current database router prevents this relation.r   )
isinstancer   rN   rO   r   concrete_modelrp   object_namerR   _stater?   r   db_for_write	__class__allow_relationr   rx   related_fieldssetattrr   getattrrw   )r!   r"   r#   rN   relatedlh_fieldrh_fields          r$   r%   z"ForwardManyToOneDescriptor.__set__  s    Z4::**0066EE&
  NN..JJOOJJ++1177CC  !!)%+%8%8&&&" ||&"("5"5OOh# ((9  "'( 
 zz..
 = jj11(D1IG
 "--gt<&*jj&?&? :"(("2"2D9:
 '+jj&?&? V"(("2"2GE8CSCS4TUV
 	

##He4
 \%:%:))%: &;r&   c                 f    t         | j                  j                  | j                  j                  ffS )z
        Pickling should return the instance attached by self.field on the
        model, not a new copy of that descriptor. Use getattr() to retrieve
        the instance directly from the model.
        )r   r   rO   rR   rS   s    r$   
__reduce__z%ForwardManyToOneDescriptor.__reduce__Z  s&     ))4::??;;;r&   r   )r'   r(   r)   __doc__rH   r   rK   r   r]   rg   re   r   r   r%   r   r*   r&   r$   rE   rE   v   sO    
$ 
 
".Y	B+
ZG
1fJ;X<r&   rE   c                   ,     e Zd ZdZ fdZ fdZ xZS )ForwardOneToOneDescriptora  
    Accessor to the related object on the forward side of a one-to-one relation.

    In the example::

        class Restaurant(Model):
            place = OneToOneField(Place, related_name='restaurant')

    ``Restaurant.place`` is a ``ForwardOneToOneDescriptor`` instance.
    c                 8   | j                   j                  j                  r|j                         }| j                   j                  j                  }|j
                  j                  D cg c]  }|j                   c}t        fd|D              snD ci c]  }|t        ||       }} |di |}|j                  j                  |j                  _        |j                  j                  |j                  _        |S t        | 9  |      S c c}w c c}w )Nc              3   &   K   | ]  }|v  
 y wr   r*   ).0r   fieldss     r$   	<genexpr>z7ForwardOneToOneDescriptor.get_object.<locals>.<genexpr>z  s     =5u=s   r*   )r   rN   parent_linkget_deferred_fieldsrO   r   concrete_fieldsr   anyr   r   addingr?   superr   )	r!   r"   deferred	rel_modelr   kwargsobjr   r   s	          @r$   r   z$ForwardOneToOneDescriptor.get_objecto  s    ::""..335H 

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

! ( 2 2


w!(++ R Os   +DDc                    t         |   ||       | j                  j                  r| j                  j                  j
                  r|j                  }|j                  D cg c]  }|j                  r|j                  r| }}|D ]S  }|j                  j                  j                  j                  j                  }|t        ||      nd }t        |||       U y y y c c}w r   )r   r%   r   primary_keyrN   r   r   r   rO   pkr   r   r   )	r!   r"   r#   optsr   inherited_pk_fieldsrel_model_pk_name	raw_valuer   s	           r$   r%   z!ForwardOneToOneDescriptor.__set__  s    %( ::!!djj&=&=&I&I>>D "11#$$);); # #
 - @$)$6$6$<$<$B$B$E$E$M$M!9>9JGE#45PT  "3Y?@ 'J!#s   "!C )r'   r(   r)   r   r   r%   __classcell__r   s   @r$   r   r   c  s    	,&@ @r&   r   c                   V    e Zd ZdZd Zed        Zd Zd ZddZ	ddZ
dd	Zd
 Zd Zy)ReverseOneToOneDescriptora  
    Accessor to the related object on the reverse side of a one-to-one
    relation.

    In the example::

        class Restaurant(Model):
            place = OneToOneField(Place, related_name='restaurant')

    ``Place.restaurant`` is a ``ReverseOneToOneDescriptor`` instance.
    c                     || _         y r   )r   )r!   r   s     r$   rH   z"ReverseOneToOneDescriptor.__init__  s     r&   c           	         t        d| j                  j                  j                  t        f| j                  j
                  j                  | j                  j
                  j                  d| j                  j                  dd      S rJ   )	rM   r   related_modelrP   rQ   rO   r(   r)   rR   rS   s    r$   rK   z3ReverseOneToOneDescriptor.RelatedObjectDoesNotExist  sk     '\\''44nE"ll00;; LL&&33LL%%!
 	
r&   c                 8    | j                   j                  |      S r   )r   r   rU   s     r$   r   z#ReverseOneToOneDescriptor.is_cached  s    ||%%h//r&   c                 ~    | j                   j                  j                  j                  |      j	                         S rW   )r   r   rY   rZ   r[   r\   s     r$   r]   z&ReverseOneToOneDescriptor.get_queryset  s/    ||))77BBBOSSUUr&   Nc                     t        j                  dt        d       || j                  |      S | j                  ||g      S r_   rb   rf   s      r$   rg   z/ReverseOneToOneDescriptor.get_prefetch_queryset  rh   r&   c                 d   |rt        |      dk7  rt        d      |r|d   n| j                         }|j                  |d          | j                  j
                  j                  }| j                  j
                  j                  }|D ci c]  } ||      | }}d| j                  j
                  j                  z  |i} |j                  di |}|D ]3  }	| ||	         }
| j                  j
                  j                  |	|
       5 |||d| j                  j                         dfS c c}w rj   )ro   rp   r]   rq   r   r   rs   rr   rR   r:   rx   ry   )r!   r=   rz   r;   r{   r|   r}   r~   r1   r   r"   s              r$   re   z0ReverseOneToOneDescriptor.get_prefetch_querysets  s5   Y1,  $-9Q<$2C2C2EYq\2||))AA**DD@IJ--t3JJDLL..333Y?"8??+U+   	CG%l7&;<HLL//B	C LL'')
 	
 Ks   D-c                 ~   || S 	 | j                   j                  |      }|C| j                  |j                  j                  d| j                   j                         d      |S # t        $ r |j                  }|d}n| j                   j                  j                  |      }	  | j                  |      j                  di |}| j                   j                  j                  ||       n+# | j                   j                  j                  $ r d}Y nw xY w| j                   j                  ||       Y w xY w)a  
        Get the related instance through the reverse relation.

        With the example above, when getting ``place.restaurant``:

        - ``self`` is the descriptor managing the ``restaurant`` attribute
        - ``instance`` is the ``place`` instance
        - ``cls`` is the ``Place`` class (unused)

        Keep in mind that ``Restaurant`` holds the foreign key to ``Place``.
        Nrm   r   rL   r*   )r   r   r   r   r   get_forward_related_filterr]   r   rx   r   rP   rK   r   r'   get_accessor_name)r!   r"   r   r   
related_pkfilter_argss         r$   r   z!ReverseOneToOneDescriptor.__get__  s-    K
	=ll33H=G$ ?00%%..0N0N0PR 
 N/  	=!J!"ll00KKHUKFd///BFFUUG LL&&77J ||11>> #"G# LL))(G<	=s5   A( (?D<("C1
'D<1%DD<DD<;D<c                    |g| j                   j                  d      }|G| j                   j                         t        || j                   j                  j
                  d       yyt        || j                   j                        slt        d|dj                  j                  d| j                   j                         d| j                   j                  j                  j                  d	      j                  j                  0t        j                  j                   |      j                  _        |j                  j                  0t        j                  |j                         |j                  _        t        j"                  |      st        d	|z        t%        fd
| j                   j                  j&                  D              }t)        | j                   j                  j*                        D ]  \  }}t        ||j,                  ||          ! | j                   j/                  |       | j                   j                  j/                  |       y)a  
        Set the related instance through the reverse relation.

        With the example above, when setting ``place.restaurant = restaurant``:

        - ``self`` is the descriptor managing the ``restaurant`` attribute
        - ``instance`` is the ``place`` instance
        - ``value`` is the ``restaurant`` instance on the right of the equal sign

        Keep in mind that ``Restaurant`` holds the foreign key to ``Place``.
        Nr   r   r   rL   r   r   rm   r   c              3   J   K   | ]  }t        |j                          y wr   )r   r   )r   r   r"   s     r$   r   z4ReverseOneToOneDescriptor.__set__.<locals>.<genexpr>J  s$       %--0s    #)r   r   r   r   r   rR   r   r   rp   r   r   r   r   r?   r   r   r   r   tuplert   	enumeratelocal_related_fieldsr   rx   )r!   r"   r#   r   r   indexr   s    `     r$   r%   z!ReverseOneToOneDescriptor.__set__  s     = ll33Hd3KG" 00: !3!3!8!8$? # E4<<#=#=> NN..LL224LL..44@@  !!)%+%8%8&&&" ||&"("5"5OOh# ((9  "'( 
  !\\//FF J !*$,,*<*<*Q*Q R Auu}}j.?@A
 LL))(E: LL//x@r&   c                 f    t         | j                  j                  | j                  j                  ffS r   )r   r   rO   rR   rS   s    r$   r   z$ReverseOneToOneDescriptor.__reduce__[  s&    ++T\\->->???r&   r   )r'   r(   r)   r   rH   r   rK   r   r]   rg   re   r   r%   r   r*   r&   r$   r   r     sJ    

 
 
 0V	B
:+ZEAN@r&   r   c                   :    e Zd ZdZd Zed        ZddZd Zd Z	y)	ReverseManyToOneDescriptora  
    Accessor to the related objects manager on the reverse side of a
    many-to-one relation.

    In the example::

        class Child(Model):
            parent = ForeignKey(Parent, related_name='children')

    ``Parent.children`` is a ``ReverseManyToOneDescriptor`` instance.

    Most of the implementation is delegated to a dynamically defined manager
    class built by ``create_forward_many_to_many_manager()`` defined below.
    c                 4    || _         |j                  | _        y r   )relr   )r!   r   s     r$   rH   z#ReverseManyToOneDescriptor.__init__p  s    YY
r&   c                     | j                   j                  }t        |j                  j                  | j                         S r   )r   r   "create_reverse_many_to_one_manager_default_managerr   r!   r   s     r$   related_manager_clsz.ReverseManyToOneDescriptor.related_manager_clst  s4    ..1**44HH
 	
r&   Nc                 ,    || S | j                  |      S )a9  
        Get the related objects through the reverse relation.

        With the example above, when getting ``parent.children``:

        - ``self`` is the descriptor managing the ``children`` attribute
        - ``instance`` is the ``parent`` instance
        - ``cls`` is the ``Parent`` class (unused)
        )r   )r!   r"   r   s      r$   r   z"ReverseManyToOneDescriptor.__get__}  s      K''11r&   c                 :    d| j                   j                         fS )Nzreverse side of a related set)r   r   rS   s    r$   _get_set_deprecation_msg_paramsz:ReverseManyToOneDescriptor._get_set_deprecation_msg_params  s    +HH&&(
 	
r&   c                 :    t        d| j                         z        )Nz@Direct assignment to the %s is prohibited. Use %s.set() instead.)	TypeErrorr   r    s      r$   r%   z"ReverseManyToOneDescriptor.__set__  s#    N2245
 	
r&   r   )
r'   r(   r)   r   rH   r   r   r   r   r%   r*   r&   r$   r   r   `  s/     
 
2

r&   r   c                 0     G fdd| t               S )z
    Create a manager for the reverse side of a many-to-one relation.

    This manager subclasses another manager, generally the default manager of
    the related model, and adds behaviors specific to many-to-one relations.
    c                        e Zd Z fdZfdZdZd Zd Zd Z fdZ	ddZ
d fd		Zdd
dZde_        dd
dZde_         fdZde_        d Zde_         fdZde_        d Zde_         fdZde_        d Zde_        W j*                  j,                  r>dd
dZde_        dd
dZde_        dd
dZde_        dd
dZde_        d Zde_        ddddZde_        ddddZde_         xZS ):create_reverse_many_to_one_manager.<locals>.RelatedManagerc                     t         |           || _        j                  | _        j
                  | _        | j
                  j                  |i| _        y r   )r   rH   r"   r   rO   r   rR   core_filters)r!   r"   r   r   s     r$   rH   zCcreate_reverse_many_to_one_manager.<locals>.RelatedManager.__init__  sB    G$DM**DJDJ!%( ;Dr&   c                    t        | j                  |      }t        |j                        } || j                        S r   )r   rO   r   r   r"   )r!   managermanager_classr   s      r$   __call__zCcreate_reverse_many_to_one_manager.<locals>.RelatedManager.__call__  s5    djj'2G>w?P?PRUVM //r&   Tc                     | j                   j                  D ]H  }t        | j                  |j                        $t        d| j                  d|j                   d       y )N"#" needs to have a value for field "z'" before this relationship can be used.)r   rt   r   r"   r   rp   )r!   r   s     r$   _check_fk_valzHcreate_reverse_many_to_one_manager.<locals>.RelatedManager._check_fk_val  s`    :: 4==%--8@$DMM, -!MM?*QS r&   c           
         | j                   xs+ t        j                  | j                  | j                        }t
        |   j                  j                  }|j                  | j                         | j                   r|j                  | j                         }d|_
         |j                  di | j                  }| j                  j                  D ]>  }t        | j                  |j                         }|	|dk(  s+|s.|j#                         c S  | j                  j$                  rV	 | j                  j&                  }t        | j                  |j                         }| j                  || j                  ii|_        |S # t(        $ ra t+        | j                  j,                  d   j.                  D cg c]"  }t        | j                  |j                         $ nc c}w c}      }Y w xY w)X
            Filter the queryset for the instance this manager is bound to.
            rm   T r*   )r0   r   db_for_readrO   r"   r   r3   !interprets_empty_strings_as_nullsrq   r-   _defer_next_filterr:   r   r   rt   r   r   nonemany_to_onetarget_fieldr   r   
path_infostarget_fields_known_related_objects)r!   r;   r?   empty_strings_as_nullr   valr   
rel_obj_ids           r$   _apply_rel_filterszMcreate_reverse_many_to_one_manager.<locals>.RelatedManager._apply_rel_filters  s    SV//

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

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

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

 !''bEB ## #,S1
## # #s   : E$$E-c                T   K    t        | j                        |d|i d {   S 7 wNr  r   r   r!   r  r  s      r$   aaddz?create_reverse_many_to_one_manager.<locals>.RelatedManager.aaddE  s'     0txx0$BTBBBB   (&(c                     | j                          | j                  || j                  j                  <   t	        j
                  | j                  | j                        }t        | j                  |      &  di |S Nrm   r*   )
r   r"   r   rR   r   r   rO   r   rZ   creater!   r   r?   RelatedManagerr   s      r$   r)  zAcreate_reverse_many_to_one_manager.<locals>.RelatedManager.createJ  s]     &*mmF4::??#$$TZZ$--HB)<DNvNNr&   c                 T   K    t        | j                        di | d {   S 7 wNr*   r   r)  r!   r   s     r$   acreatezBcreate_reverse_many_to_one_manager.<locals>.RelatedManager.acreateR  s%     3t{{3=f====r&  c                     | j                          | j                  || j                  j                  <   t	        j
                  | j                  | j                        }t        | j                  |      &  di |S r(  )
r   r"   r   rR   r   r   rO   r   rZ   get_or_creater*  s      r$   r2  zHcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_or_createW  s]     &*mmF4::??#$$TZZ$--HB)<KUfUUr&   c                 T   K    t        | j                        di | d {   S 7 wr-  r   r2  r/  s     r$   aget_or_createzIcreate_reverse_many_to_one_manager.<locals>.RelatedManager.aget_or_create_  s'     :t'9'9:DVDDDDr&  c                     | j                          | j                  || j                  j                  <   t	        j
                  | j                  | j                        }t        | j                  |      &  di |S r(  )
r   r"   r   rR   r   r   rO   r   rZ   update_or_creater*  s      r$   r7  zKcreate_reverse_many_to_one_manager.<locals>.RelatedManager.update_or_created  s^     &*mmF4::??#$$TZZ$--HB)<NXQWXXr&   c                 T   K    t        | j                        di | d {   S 7 wr-  r   r7  r/  s     r$   aupdate_or_createzLcreate_reverse_many_to_one_manager.<locals>.RelatedManager.aupdate_or_createl  s'     =t'<'<=GGGGGr&  c                T   |sy | j                          | j                  j                  | j                        }t	               }|D ]  }t        || j                        s/t        d| j                  j                  j                  d|      | j                  j                  |      |k(  r|j                  |j                         | j                  j                  j                  j                  |d| j                  d       | j                  | j!                  |      |       y )Nr  r  z is not related to rL   r  )r   r   rr   r"   setr   rO   r   r   r   rs   r   r   rN   rP   _clearr:   )r!   r  r  r  old_idsr   s         r$   removezAcreate_reverse_many_to_one_manager.<locals>.RelatedManager.removeu  s    ""$jj::4==I% C%c4::6' !%

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


- $2T2#&tzz"~'9'9';#<#%#' 5C"h ( 4 ( 4	5 $X9D9 (667 7  $*T*!7 7s   4B$D44D=c                Z   K    t        | j                        |||       d {   S 7 w)N)r  r  rE  r   r<  )r!   r  r  rE  s       r$   asetz?create_reverse_many_to_one_manager.<locals>.RelatedManager.aset  s&     0txx0dUSSSS   "+)+r   )r'   r(   r)   rH   r   do_not_call_in_templatesr   r  r  r]   rg   re   r   alters_datar%  r)  r0  r2  r5  r7  r:  r   r   r?  rB  rE  rH  r=  r<  rP  r   )r   r+  r   s   @r$   r+  r     sP   	<	0
 $( 	&	P		9"		F	S0 #' #	#J )- 	C  	O "	> #	V %)!	E &*"	Y (,$	H )-% 99>>)- ?0 "&F04 J #'G$( ( !%E+/ B "&FF "&F$( 	+4 +/u 	T  r&   r+  r   )
superclassr   r+  s    `@r$   r   r     s    l Z l \	 r&   c                   J     e Zd ZdZd fd	Zed        Zed        Zd Z	 xZ
S )ManyToManyDescriptora  
    Accessor to the related objects manager on the forward and reverse sides of
    a many-to-many relation.

    In the example::

        class Pizza(Model):
            toppings = ManyToManyField(Topping, related_name='pizzas')

    ``Pizza.toppings`` and ``Topping.pizzas`` are ``ManyToManyDescriptor``
    instances.

    Most of the implementation is delegated to a dynamically defined manager
    class built by ``create_forward_many_to_many_manager()`` defined below.
    c                 2    t         |   |       || _        y r   )r   rH   reverse)r!   r   rY  r   s      r$   rH   zManyToManyDescriptor.__init__  s    r&   c                 .    | j                   j                  S r   )r   throughrS   s    r$   r[  zManyToManyDescriptor.through  s    
 xxr&   c                     | j                   r| j                  j                  n| j                  j                  }t	        |j
                  j                  | j                  | j                         S )N)rY  )rY  r   r   rO   #create_forward_many_to_many_managerr   r   r   s     r$   r   z(ManyToManyDescriptor.related_manager_cls  sK    26,,..DHHNN2**44HHLL
 	
r&   c                     d| j                   rdndz  | j                   r| j                  j                         fS | j                  j                  fS )Nz%s side of a many-to-many setrY  forward)rY  r   r   r   rR   rS   s    r$   r   z4ManyToManyDescriptor._get_set_deprecation_msg_params  sM    + LLyi9,0LLDHH&&(
 	
 ?Cjjoo
 	
r&   )F)r'   r(   r)   r   rH   propertyr[  r   r   r   r   r   s   @r$   rW  rW    s:     
     
 

r&   rW  c                 4     G fdd| t               S )z
    Create a manager for the either side of a many-to-many relation.

    This manager subclasses another manager, generally the default manager of
    the related model, and adds behaviors specific to many-to-many relations.
    c                       e Zd Zd! fd	ZfdZdZd Zd Zd Z fdZ	d!d	Z
d! fd
	ZdddZde_        dddZde_        d Zde_        d Zde_         fdZde_        d Zde_        ddddZde_        ddddZde_        dd fd
Zde_        dddZde_        dd fd
Zde_        dddZde_        dd fd
Zde_        dddZde_        d Zd Zd ZdddZ fd Z xZ S )"?create_forward_many_to_many_manager.<locals>.ManyRelatedManagerNc                    t         |           || _        sj                  | _        j                  j                         | _        j                  j                  | _        j                  j                         | _
        j                  j                         | _        j                  | _        nj                  | _        j                  j                  | _        j                  j                         | _        j                  j                         | _
        j                  j                         | _        d| _        j                  | _        | _        | j                  j"                  j%                  | j                        | _        | j                  j"                  j%                  | j                        | _        i | _        i | _        | j&                  j.                  D ]f  \  }}| j                  d|j                  }t1        ||j2                        | j*                  |<   |j                  | j,                  |j                  <   h | j&                  j5                  |      | _        d | j6                  v r)t9        d|d| j,                  | j                     d      |j:                  "t9        d|j<                  j>                  z        y )NF__r   r   z4" before this many-to-many relationship can be used.z]%r instance needs to have a primary key value before a many-to-many relationship can be used.) r   rH   r"   rO   r   rv   query_field_namerR   prefetch_cache_namem2m_field_namesource_field_namem2m_reverse_field_nametarget_field_namesymmetricalr   r[  rY  r   	get_fieldsource_fieldr   r   pk_field_namesr   r   r   rr   related_valrp   r   r   r'   )r!   r"   r   r   core_filter_keyr   r   rY  s        r$   rH   zHcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.__init__
  s@   G$DM YY
(+		(D(D(F%+.99>>(),)A)A)C&),)I)I)K&#&??  ..
(+		%+.99+G+G+I(),)I)I)K&),)A)A)C&#( ;;DL"DL $ 2 2 < <T=S=S TD $ 2 2 < <T=S=S TD "D"$D&*&7&7&F&F C"(.2.C.CX]]"S5<XxGWGW5X!!/25=]]##HMM2C
  $00JJ8TDt'''   !4!4T5K5K!LN  {{" ?((112  #r&   c                    t        | j                  |      }t        |j                        } || j                        S r   )r   rO   r]  r   r"   )r!   r   r   r   rY  s      r$   r   zHcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.__call__<  s;    djj'2G?!!3M !$--88r&   Tc                    t        j                  | j                  | j                  fg      }t	        |t
               xs |j                         }|r(|t        j                  | j                   d|fg      z  }| j                  r[t        j                  | j                  | j                  fg      }|r(|t        j                  | j                   d|fg      z  }||z  }|S )Nr,   )	r
   r)  ri  rp  r   r   _has_filtersrk  rl  )r!   removed_valsfiltersremoved_vals_filterssymmetrical_filterss        r$   _build_remove_filterszUcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._build_remove_filtersE  s    hh!7!79I9I JKLG |X66U,:S:S:U ! $188)?)?(@&E|%T$UVV&'hh,,d.>.>?@'# ('188!334D9<HI, ' ..Nr&   c                     |j                  | j                         | j                  r|j                  | j                        }d|_         |j                         j                  di | j                  S )r   rm   Tr*   )rq   r"   r0   r-   r   _next_is_stickyr:   r   )r!   r;   s     r$   r  zRcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._apply_rel_filtersY  s_     7xx#>>$((3*.H'48++-44It7H7HIIr&   c                     	 | j                   j                  j                  | j                         y # t        t
        f$ r Y y w xY wr   )r"   r  r  rg  rQ   r   rS   s    r$   r  zZcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._remove_prefetched_objectsc  s<    77;;D<T<TU"H- s   /2 AAc                     	 | j                   j                  | j                     S # t        t        f$ r" t
        |          }| j                  |      cY S w xY wr   )r"   r  rg  rQ   r   r   r]   r  r
  s     r$   r]   zLcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_queryseti  sU    9}}>>t?W?WXX"H- 9 7/1..x889s   "& .AAc                     t        j                  dt        d       || j                  |      S | j                  ||g      S r_   rb   rf   s      r$   rg   zUcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_querysetp  r  r&   c                    |rt        |      dk7  rt        d      |r|d   nt        	|          }|j	                  |d          |j                  |j                  xs | j                        }t        |j                         | j                  |      }| j                  j                  j                  | j                        j                  j                  j                  }t         |j"                     j$                  j&                  }|j)                  j*                  D ci c]-  }d|j,                  z   ||      d ||j.                        / c}      }|fdfd	d
| j0                  d
fS c c}w )Nrk   rl   r   rm   z_prefetch_related_val_%srL   )selectc                 D     t         fdj                  D              S )Nc              3   p   K   | ]-  }|j                  t        d |j                                / yw)_prefetch_related_val_Nget_db_prep_valuer   r   )r   f
connectionresults     r$   r   z{create_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_querysets.<locals>.<lambda>.<locals>.<genexpr>  s?      %
 	 ''*@(LM"%s   36)r   r   )r  r  fks   `r$   <lambda>zhcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_querysets.<locals>.<lambda>  s!    u %
  44%   r&   c                 D     t         fdj                  D              S )Nc              3   j   K   | ]*  }|j                  t        |j                               , y wr   r  )r   r  r  r}   s     r$   r   z{create_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_querysets.<locals>.<lambda>.<locals>.<genexpr>  s1      # ''aii(@*M#s   03)r   rt   )r}   r  r  s   `r$   r  zhcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_querysets.<locals>.<lambda>  s    U #66#  r&   F)ro   rp   r   r]   rq   r-   r0   rC   r{  rf  r[  r   rm  ri  rO   db_tabler   r?   ops
quote_nameextrar   r   columnrg  )
r!   r=   rz   r;   
join_tableqnr  r  r  r   s
          @@r$   re   zVcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_querysets{  se   S^q0 #  (1y|eg6J6LH16~~hll&>dhh?H0((*D,A,A9H ##--d.D.DEB00J$X[[1J**B~~
  44	  /ii *~r!((|"55 & H  (( s   (2E<through_defaultsc                   | j                          t        j                  | j                  | j                        }t        j                  |d      5   | j                  | j                  | j                  g|d|i | j                  r+ | j                  | j                  | j                  g|d|i d d d        y # 1 sw Y   y xY w)Nrm   Fr  r  )r  r   r   r[  r"   r	   r  
_add_itemsri  rk  rl  )r!   r  r  r?   s       r$   r   zCcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.add  s    ++-$$T\\DMMJB##"> ****  &6	 ###DOO....  *:	  s   A#B??Cc                T   K    t        | j                        |d|i d {   S 7 w)Nr  r#  )r!   r  r  s      r$   r%  zDcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aadd  s3     0txx0(8   r&  c                 t    | j                           | j                  | j                  | j                  g|  y r   )r  _remove_itemsri  rk  r!   r  s     r$   r?  zFcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.remove  s2    ++-Dt55t7M7MUPTUr&   c                 N   K    t        | j                        |  d {   S 7 wr   rA  r  s     r$   rB  zGcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aremove  s"     3t{{3T::::s   %#%c           
         t        j                  | j                  | j                        }t	        j
                  |d      5  t        j                  j                  | j                  d| j                  | j                  | j                  d |       | j                          | j                  t        | 9         j                  |            }| j                  j                   j                  |      j#                  |      j%                          t        j                  j                  | j                  d| j                  | j                  | j                  d |       d d d        y # 1 sw Y   y xY w)Nrm   Fr  	pre_clearsenderactionr"   rY  rO   pk_setr-   
post_clear)r   r   r[  r"   r	   r  r   m2m_changedsendrY  rO   r  ry  r   r]   r-   r   r:   delete)r!   r?   rv  r   s      r$   rE  zEcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.clear  s   $$T\\DMMJB##"> ##((<<&!]] LL** )  //144UW5I5K5Q5QRT5UV--33B7>>wGNNP##((<<'!]] LL** )   s   DE((E1c                 R   K    t        | j                                d {   S 7 wr   rG  rS   s    r$   rH  zFcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aclear  s      2tzz24444s   '%'F)rE  r  c                   t        |      }t        j                  | j                  | j                        }t        j                  |d      5  |r#| j                           | j                  |d|i nt        | j                  |      j                  | j                  j                  j                  d            }g }|D ]x  }t        || j                        r| j                  j!                  |      d   n| j                  j#                  |      }||v r|j%                  |       h|j'                  |       z  | j$                  |   | j                  |d|i d d d        y # 1 sw Y   y xY w)Nrm   Fr  r  Tflatr   )r   r   r   r[  r"   r	   r  rE  r   r<  r-   values_listr   r   r   rO   rr   get_prep_valuer?  r  )	r!   r  rE  r  r?   r>  rM  r   fk_vals	            r$   r<  zCcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.set  sL    ;D$$T\\DMMJB##"> KJJLDHHdF5EF!

222 --::BB 3 G  "H# 	1  *#tzz: !--GGLQO!%!2!2!A!A#!F 
 "W,#NN62$OOC0	1  DKK)DHHhJ9IJ1K K Ks   DE&&E/c                Z   K    t        | j                        |||       d {   S 7 w)N)r  rE  r  rO  )r!   r  rE  r  s       r$   rP  zDcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aset  s0     0txx09I   rQ  c                    t        j                  | j                  j                  | j                        }t	        | j                  |        di |}| j                  ||       |S Nrm   r  r*   )r   r   r"   r   r   rZ   r)  r   )r!   r  r   r?   new_objManyRelatedManagerr   s        r$   r)  zFcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.create  sW    $$T]]%<%<t}}UB.0CKUfUGHHW/?H@Nr&   c                X   K    t        | j                        dd|i| d {   S 7 wNr  r*   r.  r!   r  r   s      r$   r0  zGcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.acreate'  s8     3t{{3 !15;      !*(*c                    t        j                  | j                  j                  | j                        }t	        | j                  |        di |\  }}|r| j                  ||       ||fS r  )r   r   r"   r   r   rZ   r2  r   r!   r  r   r?   r   createdr  r   s         r$   r2  zMcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_or_create.  sj    $$T]]%<%<t}}UB !3T__R5HW LC
 /?@<r&   c                X   K    t        | j                        dd|i| d {   S 7 wr  r4  r  s      r$   r5  zNcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aget_or_create;  s:     :t'9'9: !15;   r  c                    t        j                  | j                  j                  | j                        }t	        | j                  |        	 di |\  }}|r| j                  ||       ||fS r  )r   r   r"   r   r   rZ   r7  r   r  s         r$   r7  zPcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.update_or_createB  so    $$T]]%<%<t}}UB "DOOB$7)!')LC
 /?@<r&   c                X   K    t        | j                        dd|i| d {   S 7 wr  r9  r  s      r$   r:  zQcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aupdate_or_createO  s:     =t'<'<= !15;   r  c           
         ddl m} t               }| j                  j                  j                  |      }|D ]  }t        || j                        rt        j                  || j                        sGt        d|d| j                  j                  j                  d|j                  j                  d      |j                  |      d   }|t        d|d|d      |j                  |       t        ||      r/t!        d	| j                  j                  j"                  d
|      |j                  |j%                  |              |S )z[
            Return the set of ids of `objs` that the target field references.
            r   )ModelzCannot add "z": instance is on database "z", value is on database "r   z": the value for field "z	" is Noner  r  )django.db.modelsr  r<  r[  r   rm  r   rO   r   r   r"   rp   r   r?   rr   r   r   r   r  )r!   rk  r  r  
target_idsr   r   	target_ids           r$   _get_target_idszOcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._get_target_idsV  s%    /J<<--778IJL Ec4::.!00dmmD(  #DMM$8$8$;$;SZZ]]L 
 !- F Fs KA NI (("$57  NN9-U+#::++77> 
 NN<#>#>s#CD+E, r&   c                      | j                   j                  j                  |      j                  |d      j                  di || j
                  d   d|z  |i}|j                  |      S )z{
            Return the subset of ids of `objs` that aren't already assigned to
            this relationship.
            Tr  r   rn   r*   )r[  r   r-   r  r:   rp  
difference)r!   ri  rk  r?   r  valss         r$   _get_missing_target_idszWcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._get_missing_target_idsv  sv    --33B7.T: *4+;+;A+> #44j  ((..r&   c                 (   | j                   j                  j                  duxr t        |   j                  j
                  }| j                  xs || j                  k(  xr) t        j                  j                  | j                         }|||xr | fS )aw  
            Return a boolean triple of the way the add should be performed.

            The first element is whether or not bulk_create(ignore_conflicts)
            can be used, the second whether or not signals must be sent, and
            the third element is whether or not the immediate bulk insertion
            with conflicts ignored can be performed.
            F)r[  r   auto_createdr   r3   supports_ignore_conflictsrY  ri  r   r  has_listeners)r!   r?   ri  can_ignore_conflictsmust_send_signalss        r$   _get_add_planzMcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._get_add_plan  s     ""//u< GO,,FF ! K 1T5K5K K!D&&44T\\B  %!%?.?*? r&   c                j   |sy t        t        |xs i             }| j                  ||      }t        j                  | j
                  | j                        }| j                  ||      \  }}}	|	rl| j
                  j                  j                  |      j                  |D 
cg c]+  }
 | j
                  di d|z  | j                  d   d|z  |
i- c}
d       y | j                  ||||      }t        j                  |d      5  |rNt        j                   j#                  | j
                  d| j                  | j$                  | j&                  ||	       | j
                  j                  j                  |      j                  |D 
cg c]-  }
 | j
                  di |d|z  | j                  d   d|z  |
i/ c}
|       |rNt        j                   j#                  | j
                  d
| j                  | j$                  | j&                  ||	       d d d        y c c}
w c c}
w # 1 sw Y   y xY w)Nrm   z%s_idr   T)ignore_conflictsFr  pre_addr  post_addr*   )dictr   r  r   r   r[  r"   r  r   r-   bulk_createrp  r  r	   r  r   r  r  rY  rO   )r!   ri  rk  r  r  r  r?   r  r  can_fast_addr  missing_target_idss               r$   r  zJcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._add_items  sU    #$56F6L"$MN--.?FJ$$T\\DMMJBDHDVDV%EA "3\ --33B7CC *4 & %  '*; ;T=M=Ma=P '*; ;Y &* D  !%!=!=!#4b*" ##"> #$'',,#||(!% $"jj1  -  --33B7CC *<	 & % . !(*; ;T=M=Ma=P '*; ;Y	 &: D  %'',,#||)!% $"jj1  - 7# #!:	# #s&   #0HBH)2H$
=AH)$H))H2c           
      2   |sy t               }|D ]Y  }t        || j                        r0| j                  j	                  |      d   }|j                  |       I|j                  |       [ t        j                  | j                  | j                        }t        j                  |d      5  t        j                  j                  | j                  d| j                  | j                  | j                  ||       t         | E         }|j%                         rE |j'                  |      j(                  d	i d| j                  j                  j*                  z  |i}	n|}	| j-                  |	      }
| j                  j.                  j'                  |      j)                  |
      j1                          t        j                  j                  | j                  d| j                  | j                  | j                  ||       d d d        y # 1 sw Y   y xY w)
Nr   rm   Fr  
pre_remover  rn   post_remover*   )r<  r   rO   r   rr   r   r   r   r[  r"   r	   r  r   r  r  rY  r   r]   rt  r-   r:   r   ry  r   r  )r!   ri  rk  r  r>  r   r  r?   target_model_qsold_valsrv  r   s              r$   r  zMcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._remove_items  s   
  eG %c4::.!..HHMaPFKK'KK$% $$T\\DMMJB##"> ##((<<'!]] LL**" )  #('"6"8"//1?44R8??  #d&7&7&D&D&L&LLgV H  'H44X>--33B7>>wGNNP##((<<(!]] LL**" ) +  s   /EHHr   )!r'   r(   r)   rH   r   rR  ry  r  r  r]   rg   re   r   rS  r%  r?  rB  rE  rH  r<  rP  r)  r0  r2  r5  r7  r:  r  r  r  r  r  r   )r   r  r   rY  s   @r$   r  rc  	  sY   0	d	9 $( 	(	J		9		F0	d /3 	( 59 	
  	V "	; #	4 !	5 "%*T 	K@ ,1D 	
  -1 	 "48 	
 #48 		  %)!;? 	
 &*"7; 		  (,$>B 	
 )-%	@	/&	D QUE	N/	 /	r&   r  rT  )rU  r   rY  r  s    ``@r$   r]  r]    s"    W WZ Wr r&   ))r   rc   asgiref.syncr   django.core.exceptionsr   	django.dbr   r   r   r   r	   r  r
   r   r   django.db.models.functionsr   django.db.models.lookupsr   r   django.db.models.queryr   django.db.models.query_utilsr   django.db.models.utilsr   r   django.utils.deprecationr   django.utils.functionalr   r   rC   rE   r   r   r   r   rW  r]  r*   r&   r$   <module>r     s   ?B  & -  0 / 0 A + : @ = 36"3 6&*j< j<Z0@ : 0@fG@ G@T6
 6
rvr	,
5 ,
^ar&   