
    Vh                    `   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mZ ddl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 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)ManagerQWindowsignals)ColPairs)TupleIn)	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      [/home/dcms/DCMS/lib/python3.12/site-packages/django/db/models/fields/related_descriptors.py__set__z#ForeignKeyDeferredAttribute.__set__[   sh      !3!34=$**BVBVC
 JJ**8405$**,,-    N)__name__
__module____qualname__r(    r)   r'   r   r   Z   s    6r)   r   c                    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                  j%                  |d       | S c c}}w )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dbr9   r:   expr_r2   windows              r'   _filter_prefetch_querysetrG   c   s!   5
|4()45I		))B~~2''<<#1  'nn55x~~7O7O) ( ; ;" ; E R R T
T1D
 
 	*xP[22	 ;;I##% NNd3O
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'   rO   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   rR   rS   _base_manager
db_managerallr$   r]   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$   rA   r?   s      r'   get_prefetch_querysetz0ForwardManyToOneDescriptor.get_prefetch_queryset   G    $		
 ..y99**9xjAAr)   c           
      p   |rt        |      dk7  rt        d      |r|d   n| j                         }|j                  |d          | j                  j
                  }| j                  j                  }|D ci c]  } ||      | }}| j                  j                  }| j                  j                  D 	cg c]1  }	|j                  j                  |	j                        j                  3 }
}	|j                  t        t        |j                   j"                  j$                  |
|
| j                        t'        |                  }|j                  j)                          |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%   TF)len
ValueErrorrb   
_add_hintsr   get_foreign_related_valueget_local_related_valuerR   foreign_related_fieldsr5   resolve_refrV   targetfilterr   r   rS   _metadb_tablelistclear_orderingmultipleset_cached_value
cache_name)r$   rA   	querysetsr?   rel_obj_attrinstance_attrinstinstances_dictrR   r   related_fieldsrel_objr%   s                r'   rj   z1ForwardManyToOneDescriptor.get_prefetch_querysets   s   Y1,  $-9Q<$2C2C2EYq\2zz;;

::@IJ--t3JJzz.. ::
 NN&&uzz299
 
 ??NN((11""JJ	 ^$

 	%%' $$# A),w*?@--gx@A JJ!!
 	
7 K
s   8F.86F3c                 z    | j                  |      }|j                  | j                  j                  |            S Nrq   )rb   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 rP   )r   get_cached_valueKeyErrorrv   r{   get_ancestor_linkrS   r!   r   rR   r   r   nullrO   r*   rV   )r$   r%   clsr   	has_valueancestor_linkancestorrR   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 "": "rP   " must be a "" instance.rq   GCannot assign "%r": the current database router prevents this relation.r   )
isinstancer   rR   rS   r{   concrete_modelrs   object_namerV   _staterC   r   db_for_write	__class__allow_relationr   r   r   setattrr    getattrr   )r$   r%   r&   rR   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   rS   rV   rW   s    r'   
__reduce__z%ForwardManyToOneDescriptor.__reduce__a  s&     ))4::??;;;r)   r   )r*   r+   r,   __doc__rL   r   rO   r!   rb   rl   rj   r   r   r(   r   r-   r)   r'   rI   rI   {   sO    
$ 
 
".Y	B-
^G
1fJ;X<r)   rI   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>  s     =5u=s   r-   )r   rR   parent_linkget_deferred_fieldsrS   r{   concrete_fieldsr    anyr   r   addingrC   superr   )	r$   r%   deferred	rel_modelr   kwargsobjr   r   s	          @r'   r   z$ForwardOneToOneDescriptor.get_objectv  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_keyrR   r   r{   r   rS   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   j  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'   rL   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 rN   )	rQ   r   related_modelrT   rU   rS   r+   r,   rV   rW   s    r'   rO   z3ReverseOneToOneDescriptor.RelatedObjectDoesNotExist  sk     '\\''44nE"ll00;; LL&&33LL%%!
 	
r)   c                 8    | j                   j                  |      S r   )r   r!   rY   s     r'   r!   z#ReverseOneToOneDescriptor.is_cached  s    ||%%h//r)   c                 ~    | j                   j                  j                  j                  |      j	                         S r[   )r   r   r^   r_   r`   ra   s     r'   rb   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 rd   rg   rk   s      r'   rl   z/ReverseOneToOneDescriptor.get_prefetch_queryset  rm   r)   c                    |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 |}|j                  j                          |D ]3  }	| ||	         }
| j                  j
                  j                  |	|
       5 |||d| j                  j                  dfS c c}w )	Nro   rp   r   rq   %s__inTFr-   )rr   rs   rb   rt   r   r   rv   ru   rV   rz   r5   r~   r   r   )r$   rA   r   r?   r   r   r   r   r5   r   r%   s              r'   rj   z0ReverseOneToOneDescriptor.get_prefetch_querysets  sD   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   Ec                 z   || S 	 | j                   j                  |      }|?| j                  |j                  j                  d| j                   j                  d      |S # t        $ r |j                         s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``.
        Nrq   r   rP   r-   )r   r   r   
_is_pk_setr   get_forward_related_filterrb   r   r   r   rT   rO   r   r*   accessor_name)r$   r%   r   r   filter_argss        r'   r   z!ReverseOneToOneDescriptor.__get__  s'    K
	=ll33H=G" ?00%%..0J0JL 
 N-  	=&&("ll00KKHUKFd///BFFUUG LL&&77J ||11>> #"G# LL))(G<	=s6   A$ $AD:&"C/'D:/%DD:DD:9D:c                    |g| j                   j                  d      }|G| j                   j                         t        || j                   j                  j
                  d       yyt        || j                   j                        sht        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   rP   r   r   rq   r   c              3   J   K   | ]  }t        |j                          y wr   )r   r    )r   r   r%   s     r'   r   z4ReverseOneToOneDescriptor.__set__.<locals>.<genexpr>S  s$       %--0s    #)r   r   r"   r   r   rV   r   r   rs   r{   r   r   r   rC   r   r   r   r   tuplerw   	enumeratelocal_related_fieldsr    r   )r$   r%   r&   r   
related_pkindexr   s    `     r'   r(   z!ReverseOneToOneDescriptor.__set__  s     = ll33Hd3KG" 00: !3!3!8!8$? # E4<<#=#=> NN..LL..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   rS   rV   rW   s    r'   r   z$ReverseOneToOneDescriptor.__reduce__d  s&    ++T\\->->???r)   r   )r*   r+   r,   r   rL   r   rO   r!   rb   rl   rj   r   r(   r   r-   r)   r'   r   r     sK    

 
 
 0V	B
@*X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'   rL   z#ReverseManyToOneDescriptor.__init__y  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_cls}  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                 2    d| j                   j                  fS )Nzreverse side of a related set)r   r   rW   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   rL   r   r   r   r   r(   r-   r)   r'   r   r   i  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   rL   r%   r   rS   r   rV   core_filters)r$   r%   r   r   s     r'   rL   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   rS   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   rw   r   r%   r    rs   )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.
            rq   T r-   )r4   r   db_for_readrS   r%   r   r7   !interprets_empty_strings_as_nullsrt   r0   _defer_next_filterrz   r   r   rw   r   r    nonemany_to_onetarget_fieldr   r   
path_infostarget_fields_known_related_objects)r$   r?   rC   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   rR   r   rU   r   rW   s    r'   _remove_prefetched_objectszUcreate_reverse_many_to_one_manager.<locals>.RelatedManager._remove_prefetched_objects  sK    77;;JJ++66 #H- s   AA AAc                 h   | j                   j                         s,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   rs   r   r*   r  r   rR   r   rU   r   r   rb   r  )r$   r?   r   s     r'   rb   zGcreate_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   .B10B1c                     t        j                  dt        d       || j                  |      S | j                  ||g      S rd   rg   rk   s      r'   rl   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 ]E  }| j                  j                  |      r| ||         }	| j                  j                  ||	       G | j                  j                  j                  }
|||d|
dfS c c}w )Nro   rp   r   rq   F)rr   rs   r   rb   rt   r0   r4   r   rv   ru   rG   rV   r!   r   rR   r   )r$   rA   r   r?   r   r   r   r   r   r%   r   r   s              r'   rj   zQcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_prefetch_querysets  s3   S^q0 #  (1y|eg6J6LH16~~hll&>dhh?H::==L JJ@@MDMNDmD147NNN04::??IVH $ Czz++G4-l7.CDHJJ//BC 00;;J\=%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)Nrq   c                     t        | j                        s/t        dj                  j                  j                  d|       t        | j                  j                  j                         y )N'' instance expected, got )	r   rS   r   r{   r   r   r   rV   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r0   	savepointr-   )r   r  r   r   rS   r%   r   r   rC   rs   appendr   r^   r0   rz   updater   rV   r	   atomicsave)r$   r  objsrC   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.aaddN  s'     0txx0$BTBBBB   (&(c                    | j                          | j                          | j                  || j                  j                  <   t        j                  | j                  | j                        }t        | j                  |      *  di |S Nrq   r-   )r   r  r%   r   rV   r   r   rS   r   r_   creater$   r   rC   RelatedManagerr   s      r'   r/  zAcreate_reverse_many_to_one_manager.<locals>.RelatedManager.createS  si     ++-&*mmF4::??#$$TZZ$--HB)<DNvNNr)   c                 T   K    t        | j                        di | d {   S 7 wNr-   r   r/  r$   r   s     r'   acreatezBcreate_reverse_many_to_one_manager.<locals>.RelatedManager.acreate\  s%     3t{{3=f====r,  c                     | j                          | j                  || j                  j                  <   t	        j
                  | j                  | j                        }t        | j                  |      &  di |S r.  )
r   r%   r   rV   r   r   rS   r   r_   get_or_creater0  s      r'   r8  zHcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_or_createa  s]     &*mmF4::??#$$TZZ$--HB)<KUfUUr)   c                 T   K    t        | j                        di | d {   S 7 wr3  r   r8  r5  s     r'   aget_or_createzIcreate_reverse_many_to_one_manager.<locals>.RelatedManager.aget_or_createi  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   rV   r   r   rS   r   r_   update_or_creater0  s      r'   r=  zKcreate_reverse_many_to_one_manager.<locals>.RelatedManager.update_or_createn  s^     &*mmF4::??#$$TZZ$--HB)<NXQWXXr)   c                 T   K    t        | j                        di | d {   S 7 wr3  r   r=  r5  s     r'   aupdate_or_createzLcreate_reverse_many_to_one_manager.<locals>.RelatedManager.aupdate_or_createv  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 rP   r  )r   r   ru   r%   setr   rS   r   r{   r   rv   r&  r   rR   rT   _clearrz   )r$   r  r$  r
  old_idsr   s         r'   removezAcreate_reverse_many_to_one_manager.<locals>.RelatedManager.remove  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   rE  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   rC  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   rK  rJ  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)Nrq   Fr  )update_fieldsr-   )r  r   r   rS   r%   r0   r!  r   rV   r	   r"  r   r#  )r$   r?   r  rC   r   s        r'   rC  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  rK  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)Nrq   Fr  r  r  )r   r   r   r   r   r   rS   r%   r	   r"  rK  r&  rB  r0   r`   rE  r   )r$   r$  r  rK  rC   old_objsnew_objsr   s           r'   rB  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  rK  r   rB  )r$   r$  r  rK  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,   rL   r   do_not_call_in_templatesr   r  r  rb   rl   rj   r&  alters_datar+  r/  r6  r8  r;  r=  r@  r   r   rE  rH  rK  rN  rC  rB  rV  r   )r   r1  r   s   @r'   r1  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)   r1  r   )
superclassr   r1  s    `@r'   r   r     s    m Z m ^	 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   rL   reverse)r$   r   r_  r   s      r'   rL   zManyToManyDescriptor.__init__  s    r)   c                 .    | j                   j                  S r   )r   throughrW   s    r'   ra  zManyToManyDescriptor.through  s    
 xxr)   c                     | j                   r| j                  j                  n| j                  j                  }t	        |j
                  j                  | j                  | j                         S )N)r_  )r_  r   r   rS   #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 setr_  forward)r_  r   r   r   rV   rW   s    r'   r   z4ManyToManyDescriptor._get_set_deprecation_msg_params  sI    + LLyi9&*llDHH""
 	
 9=


 	
r)   )F)r*   r+   r,   r   rL   propertyra  r   r   r   r   r   s   @r'   r]  r]    s:     
     
 

r)   r]  c                 8      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d Z	 fd	Z
d%d
Zd% fd	Zed        Z fdZ 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;                         s"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   rL   r%   rS   r   related_query_namequery_field_namerV   prefetch_cache_namem2m_field_namesource_field_namem2m_reverse_field_nametarget_field_namesymmetricalr   ra  r_  r{   	get_fieldsource_fieldr  r   pk_field_namesr   r   r    ru   related_valrs   r   r   r*   )r$   r%   r   r   core_filter_keyr   r   r_  s        r'   rL   zHcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.__init__  sB   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   rS   rc  r   r%   )r$   r   r   r   r_  s      r'   r   zHcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.__call__F  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/  rp  rw  r   r   _has_filtersrr  rs  )r$   removed_valsfiltersremoved_vals_filterssymmetrical_filterss        r'   _build_remove_filterszUcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._build_remove_filtersO  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   rq   Tr-   )rt   r%   r4   r0   r  _next_is_stickyrz   r   )r$   r?   s     r'   r  zRcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._apply_rel_filtersc  s_     7xx#>>$((3*.H'48++-44It7H7HIIr)   c                 t    	 | j                   j                  | j                     S # t        t        f$ r Y y w xY wr   )r%   r  rn  rU   r   rW   s    r'   get_prefetch_cachezRcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_cachem  s9    }}>>t?W?WXX"H- s   "% 77c                     	 | j                   j                  j                  | j                         y # t        t
        f$ r Y y w xY wr   )r%   r  r  rn  rU   r   rW   s    r'   r  zZcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._remove_prefetched_objectss  s<    77;;D<T<TU"H- s   /2 AAc                 j    | j                         x}|S t        | 	         }| j                  |      S r   )r  r   rb   r  )r$   cacher?   r   s      r'   rb   zLcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_querysety  s:    0022? 7/1..x88r)   c                     t        j                  dt        d       || j                  |      S | j                  ||g      S rd   rg   rk   s      r'   rl   zUcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_queryset  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                  }t        |j                      j"                  j$                  }|j'                  j(                  D ci c]-  }d|j*                  z   ||      d ||j,                        / c}      }|fdfd	d
| j.                  d
fS c c}w )Nro   rp   r   rq   z_prefetch_related_val_%srP   )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   rw   )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)rr   rs   r   rb   rt   r0   r4   rG   rm  ra  r{   rt  rp  rS   r|   r   rC   ops
quote_nameextrar   r    columnrn  )
r$   rA   r   r?   
join_tableqnr  r  r  r   s
          @@r'   rj   zVcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_querysets  s\   S^q0 #  (1y|eg6J6LH16~~hll&>dhh?H0$//H ##--d.D.DEB00J$X[[1J**B~~
  44	  /ii *~r!((|"55 & H  (( s   2E.c                    | j                   j                  sy t        j                  | j                  | j
                        }t        |   j                  j                  sy d| j
                  i}| j                  j                  j                  ||      }| j                  | j                  d   i}| j                   j                  rd|d| j                  z  <    |j                  di |S )Nrq   r%   r\   r   Fz
%s__isnullr-   )r  db_constraintr   r   ra  r%   r   r7   supports_foreign_keysr^   r_   rp  rw  r   rr  rz   )r$   rC   r]   r   r}  s        r'   constrained_targetzRcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.constrained_target  s    
 $$22##DLL4==IBr?++AA/Ell00;;Be;LG--t/?/?/BCG   %%AFt'='==>!7>>,G,,r)   c                     t         u r.| j                         | j                  x}|j                         S t        |          S r   )r
   r  r  existsr   r$   r  r   r[  s     r'   r  zFcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.exists  sH    g%++-5+/+B+BB'O)0022w~''r)   c                     t         u r.| j                         | j                  x}|j                         S t        |          S r   )r
   r  r  countr   r  s     r'   r  zEcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.count  sG    g%++-5+/+B+BB'O)//11w}&r)   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)Nrq   Fr  r  )r  r   r   ra  r%   r	   r"  
_add_itemsrp  rr  rs  )r$   r  r$  rC   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_itemsrp  rr  r$   r$  s     r'   rE  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   rG  r  s     r'   rH  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)Nrq   Fr  	pre_clearsenderactionr%   r_  rS   pk_setr0   
post_clear)r   r   ra  r%   r	   r"  r   m2m_changedsendr_  rS   r  r  r   rb   r0   r   rz   delete)r$   rC   r}  r   s      r'   rK  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   rM  rW   s    r'   rN  zFcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aclear(  s      2tzz24444s   '%'F)rK  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)Nrq   Fr  r  Tflatr   )r   r   r   ra  r%   r	   r"  rK  r&  rB  r0   values_listr  r    r   rS   ru   get_prep_valuerE  r   )	r$   r$  rK  r  rC   rD  rS  r   fk_vals	            r'   rB  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$  rK  r  rU  )r$   r$  rK  r  s       r'   rV  zDcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.asetO  s0     0txx09I   rW  c                    t        j                  | j                  j                  | j                        }t	        | j                  |        di |}| j                  ||       |S Nrq   r  r-   )r   r   r%   r   r   r_   r/  r&  )r$   r  r   rC   new_objManyRelatedManagerr   s        r'   r/  zFcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.createV  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-   r4  r$   r  r   s      r'   r6  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   r_   r8  r&  r$   r  r   rC   r   createdr  r   s         r'   r8  zMcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_or_createe  sj    $$T]]%<%<t}}UB !3T__R5HW LC
 /?@<r)   c                X   K    t        | j                        dd|i| d {   S 7 wr  r:  r  s      r'   r;  zNcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aget_or_creater  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   r_   r=  r&  r  s         r'   r=  zPcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.update_or_createy  so    $$T]]%<%<t}}UB "DOOB$7)!')LC
 /?@<r)   c                X   K    t        | j                        dd|i| d {   S 7 wr  r?  r  s      r'   r@  zQcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aupdate_or_create  s:     =t'<'<= !15;   r  c           
         ddl m} t               }| j                  j                  j                  |      }|D ]  }t        || j                        rt        j                  || j                        sGt        d|d| j                  j                  j                  d|j                  j                  d      |j                  |      d   }|t        d|d|d      |j                  |       t        ||      r/t!        d	| j                  j                  j"                  d
|      |j                  |j%                  |              |S )z[
            Return the set of ids of `objs` that the target field references.
            r   )ModelzCannot add "z": instance is on database "z", value is on database "r   z": the value for field "z	" is Noner  r  )django.db.modelsr  rB  ra  r{   rt  r   rS   r   r   r%   rs   r   rC   ru   r&  r   r   r  )r$   rr  r$  r  
target_idsr  r   	target_ids           r'   _get_target_idszOcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._get_target_ids  s%    /J<<--778IJL Ec4::.!00dmmD(  #DMM$8$8$;$;SZZ]]L 
 !- F Fs KA NI (("$57  NN9-U+#::++77> 
 NN<#>#>s#CD+E, r)   c                      | j                   j                  j                  |      j                  |d      j                  di || j
                  d   d|z  |i}|j                  |      S )z{
            Return the subset of ids of `objs` that aren't already assigned to
            this relationship.
            Tr  r   r   r-   )ra  r   r0   r  rz   rw  
difference)r$   rp  rr  rC   r  valss         r'   _get_missing_target_idszWcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._get_missing_target_ids  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)ra  r{   auto_createdr   r7   supports_ignore_conflictsr_  rp  r   r  has_listeners)r$   rC   rp  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)Nrq   z%s_idr   T)ignore_conflictsFr  pre_addr  post_addr-   )dictr   r  r   r   ra  r%   r  r   r0   bulk_createrw  r  r	   r"  r   r  r  r_  rS   )r$   rp  rr  r  r$  r  rC   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   rq   Fr  
pre_remover  r   post_remover-   )rB  r   rS   r  ru   r&  r   r   ra  r%   r	   r"  r   r  r  r_  r   rb   r{  r0   rz   r    r  r   r  )r$   rp  rr  r$  rD  r   r  rC   target_model_qsold_valsr}  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,   rL   r   rX  r  r  r  r  rb   rl   rj   rf  r  r  r  r&  rY  r+  rE  rH  rK  rN  rB  rV  r/  r6  r8  r;  r=  r@  r  r  r  r  r  r   )r   r  r   r_  r[  s   @r'   r  ri    s|   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  rZ  )r[  r   r_  r  s   ```@r'   rc  rc    s"    D	 D	Z D	L r)   ).r   rh   asgiref.syncr   django.core.exceptionsr   	django.dbr   r   r   r   r	   r  r
   r   r   r   django.db.models.expressionsr   %django.db.models.fields.tuple_lookupsr   django.db.models.functionsr   django.db.models.lookupsr   r   django.db.models.queryr   django.db.models.query_utilsr   django.db.models.utilsr   r   django.utils.deprecationr   django.utils.functionalr   r   rG   rI   r   r   r   r   r]  rc  r-   r)   r'   <module>r     s   ?B  & -  9 8 1 9 0 A + : @ = 36"3 60l< l<^0@ : 0@fI@ I@X6
 6
rwt	,
5 ,
^N	r)   