
    +{BiZ                     R   d Z ddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
 ddlmZmZmZ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!  G d de      Z"d Z#d Z$ G d d      Z% G d de%      Z& G d d      Z' G d d      Z(d Z) G d de(      Z*d Z+y)a
  
Accessors for related objects.

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

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

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

    class Parent(Model):
        pass

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

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

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

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

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

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

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

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

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

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

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

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

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

   If you're looking for ``ForwardManyToManyDescriptor`` or
   ``ReverseManyToManyDescriptor``, use ``ManyToManyDescriptor`` instead.
    )sync_to_async)
FieldError)DEFAULT_DB_ALIASNotSupportedErrorconnectionsroutertransaction)ManagerQWindowsignals)ColPairs)TupleIn)	RowNumber)GreaterThanLessThanOrEqual)QuerySet)DeferredAttribute)
AltersDataresolve_callables)cached_propertyc                       e Zd Zd Zy)ForeignKeyDeferredAttributec                    |j                   j                  | j                  j                        |k7  r6| j                  j	                  |      r| j                  j                  |       ||j                   | j                  j                  <   y N)__dict__getfieldattname	is_cacheddelete_cached_valueselfinstancevalues      `/var/www/python/venv/lib/python3.12/site-packages/django/db/models/fields/related_descriptors.py__set__z#ForeignKeyDeferredAttribute.__set__X   sh      !3!34=$**BVBVC
 JJ**8405$**,,-    N)__name__
__module____qualname__r'    r(   r&   r   r   W   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dbr8   r9   expr_r1   windows              r&   _filter_prefetch_querysetrF   `   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              #      K   |}|B|j                   j                  |       }|s|d f y |j                  |d       }||f |}|Ay y wr   )_metaget_ancestor_linkget_cached_value)modelstarting_instancecurrent_instanceancestor_linkancestors        r&   _traverse_ancestorsrP   x   se     (

&(..@@G"D(( 112BDI((# 
&s   AA
A
c                   T    e Zd ZdZd Zed        Zd Zd ZddZ	d Z
dd	Zd
 Zd Zy)ForwardManyToOneDescriptoraJ  
    Accessor to the related object on the forward side of a many-to-one or
    one-to-one (via ForwardOneToOneDescriptor subclass) relation.

    In the example::

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

    ``Child.parent`` is a ``ForwardManyToOneDescriptor`` instance.
    c                     || _         y r   )r   )r#   field_with_rels     r&   __init__z#ForwardManyToOneDescriptor.__init__   s	    #
r(   c           	      *   t        d| j                  j                  j                  j                  t
        f| j                  j                  j                  | j                  j                  j                  d| j                  j                  dd      S NRelatedObjectDoesNotExist.z.RelatedObjectDoesNotExist)r*   r+   )	typer   remote_fieldrK   DoesNotExistAttributeErrorr*   r+   namer#   s    r&   rX   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   r[   rK   _base_manager
db_managerallr#   re   s     r&   get_querysetz'ForwardManyToOneDescriptor.get_queryset   s5    zz&&,,::EEEERVVXXr(   Nc           
      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
ValueErrorrj   
_add_hintsr   get_foreign_related_valueget_local_related_valuer[   foreign_related_fieldsr4   resolve_refr^   targetfilterr   r   rK   rH   db_tablelistclear_orderingmultipleset_cached_value
cache_name)r#   r@   	querysetsr>   rel_obj_attrinstance_attrinstinstances_dictr[   r   related_fieldsrel_objr$   s                r&   get_prefetch_querysetsz1ForwardManyToOneDescriptor.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 Nrn   )rj   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}d| j                   j                  |      v}|rN| j                   j                  }t        ||      D ])  \  }}|s	| j                   j                  |d      }|) n |G|rE| j                  |      }| j                   j                  }|j                  s|j                  ||       | j                   j                  ||       Y Cw 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 rY   )r   rJ   KeyErrorrs   rK   rP   r   r[   r{   r|   nullrX   r)   r^   )	r#   r$   clsr   	has_valuerK   rM   rO   r[   s	            r&   __get__z"ForwardManyToOneDescriptor.__get__   sM    K
	;jj11(;G2 ?4::??00#'::#3#3#<#<djjooN  N;  	;GDJJ$F$Fx$PPI

((2EeX2V ".$h #'**"="=hPT"="U".!" 9//(3#zz66 $,, 11'8DJJ''':-	;s   B AE%E%9A(E%$E%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 "": "rY   " must be a "" instance.rn   GCannot assign "%r": the current database router prevents this relation.r   )
isinstancer   r[   rK   rH   concrete_modelrp   object_namer^   _staterB   r   db_for_write	__class__allow_relationrJ   r|   r   setattrr   getattrr{   )r#   r$   r%   r[   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   rK   r^   r_   s    r&   
__reduce__z%ForwardManyToOneDescriptor.__reduce__]  s&     ))4::??;;;r(   r   )r)   r*   r+   __doc__rU   r   rX   r    rj   r   r   r   r'   r   r,   r(   r&   rR   rR      sI    
$ 
 
".Y-
^G
/bJ;X<r(   rR   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   r[   parent_linkget_deferred_fieldsrK   rH   concrete_fieldsr   anyr   r   addingrB   superr   )	r#   r$   deferred	rel_modelr   kwargsobjr   r   s	          @r&   r   z$ForwardOneToOneDescriptor.get_objects  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_keyr[   r   rH   r   rK   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   f  s    
,&@ @r(   r   c                   N    e Zd ZdZd Zed        Zd Zd ZddZ	ddZ
d	 Zd
 Zy)ReverseOneToOneDescriptora  
    Accessor to the related object on the reverse side of a one-to-one
    relation.

    In the example::

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

    ``Place.restaurant`` is a ``ReverseOneToOneDescriptor`` instance.
    c                     || _         y r   )r   )r#   r   s     r&   rU   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 rW   )	rZ   r   related_modelr\   r]   rK   r*   r+   r^   r_   s    r&   rX   z3ReverseOneToOneDescriptor.RelatedObjectDoesNotExist  sk     '\\''44nE"ll00;; LL&&33LL%%!
 	
r(   c                 8    | j                   j                  |      S r   )r   r    ra   s     r&   r    z#ReverseOneToOneDescriptor.is_cached  s    ||%%h//r(   c                 ~    | j                   j                  j                  j                  |      j	                         S rc   )r   r   rf   rg   rh   ri   s     r&   rj   z&ReverseOneToOneDescriptor.get_queryset  s/    ||))77BBBOSSUUr(   Nc                    |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 )	Nrl   rm   r   rn   %s__inTFr,   )ro   rp   rj   rq   r   r   rs   rr   r^   rw   r4   rz   r|   r}   )r#   r@   r~   r>   r   r   r   r   r4   r   r$   s              r&   r   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``.
        Nrn   r   rY   r,   )r   rJ   r   
_is_pk_setr   get_forward_related_filterrj   r   r|   r   r\   rX   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   rY   r   r   rn   r   c              3   J   K   | ]  }t        |j                          y wr   )r   r   )r   r   r$   s     r&   r   z4ReverseOneToOneDescriptor.__set__.<locals>.<genexpr>F  s$       %--0s    #)r   rJ   r!   r   r   r^   r   r   rp   rH   r   r   r   rB   r   r   r   r   tuplert   	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   rK   r^   r_   s    r&   r   z$ReverseOneToOneDescriptor.__reduce__X  s&    ++T\\->->???r(   r   )r)   r*   r+   r   rU   r   rX   r    rj   r   r   r'   r   r,   r(   r&   r   r     sE    

 
 
 0V
@*XGAR@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_reverse_many_to_one_manager()`` defined below.
    c                 4    || _         |j                  | _        y r   )relr   )r#   r   s     r&   rU   z#ReverseManyToOneDescriptor.__init__m  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_clsq  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__z  s      K''11r(   c                 2    d| j                   j                  fS )Nzreverse side of a related set)r   r   r_   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   rU   r   r   r   r   r'   r,   r(   r&   r   r   ]  s/     
 
2

r(   r   c                 0     G fdd| t               S )z
    Create a manager for the reverse side of a many-to-one relation.

    This manager subclasses another manager, generally the default manager of
    the related model, and adds behaviors specific to many-to-one relations.
    c                       e Zd Z fdZfdZdZd Zd Zd Z fdZ	d fd	Z
dd	d
Zde_        dd	dZde_         fdZde_        d Zde_         fdZde_        d Zde_         fdZde_        d Zde_        W j(                  j*                  r>dd	dZde_        dd	dZde_        dd	dZde_        dd	dZde_        d Zde_        ddddZde_        ddddZde_         xZS ):create_reverse_many_to_one_manager.<locals>.RelatedManagerc                     t         |           || _        j                  | _        j
                  | _        | j
                  j                  |i| _        y r   )r   rU   r$   r   rK   r   r^   core_filters)r#   r$   r   r   s     r&   rU   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   rK   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.
            rn   T r,   )r3   r   db_for_readrK   r$   r   r6   !interprets_empty_strings_as_nullsrq   r/   _defer_next_filterrw   r   r   rt   r   r   nonemany_to_onetarget_fieldr   r   
path_infostarget_fields_known_related_objects)r#   r>   rB   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   r[   r}   r]   r   r_   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   rp   r   r)   r  r   r[   r}   r]   r   r   rj   r  )r#   r>   r   s     r&   rj   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                    |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 )Nrl   rm   r   rn   F)ro   rp   r   rj   rq   r/   r3   r   rs   rr   rF   r^   r    r|   r[   r}   )r#   r@   r~   r>   r   r   r   r   r   r$   r}   r   s              r&   r   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)Nrn   c                     t        | j                        s/t        dj                  j                  j                  d|       t        | j                  j                  j                         y )N'' instance expected, got )	r   rK   r   rH   r   r   r   r^   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   rK   r$   r   r   rB   rp   appendr   rf   r/   rw   updater   r^   r	   atomicsave)r#   r  objsrB   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.aadd7  s'     0txx0$BTBBBB   (&(c                    | j                          | j                          | j                  || j                  j                  <   t        j                  | j                  | j                        }t        | j                  |      *  di |S Nrn   r,   )r   r  r$   r   r^   r   r   rK   r   rg   creater#   r   rB   RelatedManagerr   s      r&   r%  zAcreate_reverse_many_to_one_manager.<locals>.RelatedManager.create<  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.acreateE  s%     3t{{3=f====r"  c                     | j                          | j                  || j                  j                  <   t	        j
                  | j                  | j                        }t        | j                  |      &  di |S r$  )
r   r$   r   r^   r   r   rK   r   rg   get_or_creater&  s      r&   r.  zHcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_or_createJ  s]     &*mmF4::??#$$TZZ$--HB)<KUfUUr(   c                 T   K    t        | j                        di | d {   S 7 wr)  r   r.  r+  s     r&   aget_or_createzIcreate_reverse_many_to_one_manager.<locals>.RelatedManager.aget_or_createR  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   r^   r   r   rK   r   rg   update_or_creater&  s      r&   r3  zKcreate_reverse_many_to_one_manager.<locals>.RelatedManager.update_or_createW  s^     &*mmF4::??#$$TZZ$--HB)<NXQWXXr(   c                 T   K    t        | j                        di | d {   S 7 wr)  r   r3  r+  s     r&   aupdate_or_createzLcreate_reverse_many_to_one_manager.<locals>.RelatedManager.aupdate_or_create_  s'     =t'<'<=GGGGGr"  c                T   |sy | j                          | j                  j                  | j                        }t	               }|D ]  }t        || j                        s/t        d| j                  j                  j                  d|      | j                  j                  |      |k(  r|j                  |j                         | j                  j                  j                  j                  |d| j                  d       | j                  | j!                  |      |       y )Nr  r  z is not related to rY   r  )r   r   rr   r$   setr   rK   r   rH   r   rs   r  r   r[   r\   _clearrw   )r#   r  r  r  old_idsr   s         r&   removezAcreate_reverse_many_to_one_manager.<locals>.RelatedManager.removeh  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   r9  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   rA  r@  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)Nrn   Fr  )update_fieldsr,   )r  r   r   rK   r$   r/   r  r   r^   r	   r  r   r  )r#   r>   r  rB   r   s        r&   r9  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  rA  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)Nrn   Fr  r  r  )r   r   r   r   r   r   rK   r$   r	   r  rA  r  r8  r/   rh   r;  r  )r#   r  r  rA  rB   old_objsnew_objsr   s           r&   r8  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  rA  r   r8  )r#   r  r  rA  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+   rU   r   do_not_call_in_templatesr   r  r  rj   r   r  alters_datar!  r%  r,  r.  r1  r3  r6  r   r   r;  r>  rA  rD  r9  r8  rL  r   )r   r'  r   s   @r&   r'  r     sJ   	<	0
 $( 	&	P		9"	S0 #' #	#J )- 	C  	O "	> #	V %)!	E &*"	Y (,$	H )-% 99>>)- ?0 "&F04 J #'G$( ( !%E+/ B "&FF "&F$( 	+4 +/u 	T  r(   r'  r   )
superclassr   r'  s    `@r&   r   r     s    b Z b H	 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   rU   reverse)r#   r   rU  r   s      r&   rU   zManyToManyDescriptor.__init__  s    r(   c                 .    | j                   j                  S r   )r   throughr_   s    r&   rW  zManyToManyDescriptor.through  s    
 xxr(   c                     | j                   r| j                  j                  n| j                  j                  }t	        |j
                  j                  | j                  | j                         S )N)rU  )rU  r   r   rK   #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 setrU  forward)rU  r   r   r   r^   r_   s    r&   r   z4ManyToManyDescriptor._get_set_deprecation_msg_params  sI    + LLyi9&*llDHH""
 	
 9=


 	
r(   )F)r)   r*   r+   r   rU   propertyrW  r   r   r   r   r   s   @r&   rS  rS    s:     
     
 

r(   rS  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fdZfdZ	 f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   rU   r$   rK   r   related_query_namequery_field_namer^   prefetch_cache_namem2m_field_namesource_field_namem2m_reverse_field_nametarget_field_namesymmetricalr   rW  rU  rH   	get_fieldsource_fieldr   r   pk_field_namesr   r   r   rr   related_valrp   r   r   r)   )r#   r$   r   r   core_filter_keyr   r   rU  s        r&   rU   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   rK   rY  r   r$   )r#   r   r   r   rU  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%  rf  rm  r   r   _has_filtersrh  ri  )r#   removed_valsfiltersremoved_vals_filterssymmetrical_filterss        r&   _build_remove_filterszUcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._build_remove_filters8  s    hh!7!79I9I JKLG |X66U,:S:S:U ! $188)?)?(@&E|%T$UVV&'hh,,d.>.>?@'# ('188!334D9<HI, ' ..Nr(   c                     |j                  | j                         | j                  r|j                  | j                        }d|_         |j                         j                  di | j                  S )r   rn   Tr,   )rq   r$   r3   r/   r   _next_is_stickyrw   r   )r#   r>   s     r&   r  zRcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._apply_rel_filtersL  s_     7xx#>>$((3*.H'48++-44It7H7HIIr(   c                     t        j                  j                  | j                        D ]   \  }}	 |j                  | j
                     c S  y # t        t        f$ r Y 5w xY wr   )rP   r   rK   r$   r  rd  r]   r   r#   r$   rD   r   s      r&   get_prefetch_cachezRcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_cacheV  se      3399??DMMR !#==d>V>VWW
  '1 s   AA"!A"c                     t        j                  j                  | j                        D ]+  \  }}	 |j                  j                  | j                         - y # t        t        f$ r Y @w xY wr   )	rP   r   rK   r$   r  r  rd  r]   r   rz  s      r&   r  zZcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._remove_prefetched_objects`  sc      3399??DMMR !66::4;S;ST '1 s   %AA-,A-c                 j    | j                         x}|S t        | 	         }| j                  |      S r   )r{  r   rj   r  )r#   cacher>   r   s      r&   rj   zLcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_queryseti  s:    0022? 7/1..x88r(   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 )Nrl   rm   r   rn   z_prefetch_related_val_%srY   )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   rj   rq   r/   r3   rF   rc  rW  rH   rj  rf  rK   rx   r   rB   ops
quote_nameextrar   r   columnrd  )
r#   r@   r~   r>   
join_tableqnr  r  r  r   s
          @@r&   r   zVcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_querysetsp  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 )Nrn   r$   rd   r   Fz
%s__isnullr,   )r   db_constraintr   r   rW  r$   r   r6   supports_foreign_keysrf   rg   rf  rm  r   rh  rw   )r#   rB   re   r   rs  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   rQ  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)Nrn   Fr  r  )r  r   r   rW  r$   r	   r  
_add_itemsrf  rh  ri  )r#   r  r  rB   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_itemsrf  rh  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   r=  r  s     r&   r>  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)Nrn   Fr  	pre_clearsenderactionr$   rU  rK   pk_setr/   
post_clear)r   r   rW  r$   r	   r  r   m2m_changedsendrU  rK   r  rv  r   rj   r/   r   rw   delete)r#   rB   rs  r   s      r&   rA  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   rC  r_   s    r&   rD  zFcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aclear  s      2tzz24444s   '%'F)rA  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)Nrn   Fr  r  Tflatr   )r   r   r   rW  r$   r	   r  rA  r  r8  r/   values_listr   r   r   rK   rr   get_prep_valuer;  r  )	r#   r  rA  r  rB   r:  rI  r   fk_vals	            r&   r8  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  rA  r  rK  )r#   r  rA  r  s       r&   rL  zDcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aset4  s0     0txx09I   rM  c                    t        j                  | j                  j                  | j                        }t	        | j                  |        di |}| j                  ||       |S Nrn   r  r,   )r   r   r$   r   r   rg   r%  r  )r#   r  r   rB   new_objManyRelatedManagerr   s        r&   r%  zFcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.create;  sW    $$T]]%<%<t}}UB.0CKUfUGHHW/?H@Nr(   c                X   K    t        | j                        dd|i| d {   S 7 wNr  r,   r*  r#   r  r   s      r&   r,  zGcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.acreateC  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   rg   r.  r  r#   r  r   rB   r   createdr  r   s         r&   r.  zMcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_or_createJ  sj    $$T]]%<%<t}}UB !3T__R5HW LC
 /?@<r(   c                X   K    t        | j                        dd|i| d {   S 7 wr  r0  r  s      r&   r1  zNcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aget_or_createW  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   rg   r3  r  r  s         r&   r3  zPcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.update_or_create^  so    $$T]]%<%<t}}UB "DOOB$7)!')LC
 /?@<r(   c                X   K    t        | j                        dd|i| d {   S 7 wr  r5  r  s      r&   r6  zQcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aupdate_or_createk  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  r8  rW  rH   rj  r   rK   r   r   r$   rp   r   rB   rr   r  r   r   r  )r#   rh  r  r  
target_idsr   r   	target_ids           r&   _get_target_idszOcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._get_target_idsr  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,   )rW  r   r/   r  rw   rm  
difference)r#   rf  rh  rB   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)rW  rH   auto_createdr   r6   supports_ignore_conflictsrU  rf  r   r  has_listeners)r#   rB   rf  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)Nrn   z%s_idr   T)ignore_conflictsFr  pre_addr  post_addr,   )dictr   r  r   r   rW  r$   r  r   r/   bulk_createrm  r  r	   r  r   r  r  rU  rK   )r#   rf  rh  r  r  r  rB   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   rn   Fr  
pre_remover  r   post_remover,   )r8  r   rK   r   rr   r  r   r   rW  r$   r	   r  r   r  r  rU  r   rj   rq  r/   rw   r   rv  r   r  )r#   rf  rh  r  r:  r   r  rB   target_model_qsold_valsrs  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+   rU   r   rN  rv  r  r{  r  rj   r   r\  r  r  r  r  rO  r!  r;  r>  rA  rD  r8  rL  r%  r,  r.  r1  r3  r6  r  r  r  r  r  r   )r   r  r   rU  rQ  s   @r&   r  r_    sv   0	d	9 $( 	(	J			90	d 
	- 
	-$	(	' /3 	( 59 	
  	V "	; #	4 !	5 "%*T 	K@ ,1D 	
  -1 	 "48 	
 #48 		  %)!;? 	
 &*"7; 		  (,$>B 	
 )-%	@	/&	D QUE	N/	 /	r(   r  rP  )rQ  r   rU  r  s   ```@r&   rY  rY    s"    @	 @	Z @	D r(   N),r   asgiref.syncr   django.core.exceptionsr   	django.dbr   r   r   r   r	   r  r
   r   r   r   django.db.models.expressionsr   %django.db.models.fields.tuple_lookupsr   django.db.models.functionsr   django.db.models.lookupsr   r   django.db.models.queryr   django.db.models.query_utilsr   django.db.models.utilsr   r   django.utils.functionalr   r   rF   rP   rR   r   r   r   r   rS  rY  r,   r(   r&   <module>r     s   ?B ' -  9 8 1 9 0 A + : @ 36"3 60	$_< _<D1@ : 1@h@@ @@F6
 6
rl^	,
5 ,
^J	r(   