
    g                    H   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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)	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      /var/www/python.vincentserveurtest.ovh/public_html/venv/lib/python3.12/site-packages/django/db/models/fields/related_descriptors.py__set__z#ForeignKeyDeferredAttribute.__set__Y   sh      !3!34=$**BVBVC
 JJ**8405$**,,-    N)__name__
__module____qualname__r&    r'   r%   r   r   X   s    6r'   r   c                 d   t        di | d|i}| j                  xs t        }| j                  j                  rt
        |   j                  j                  st        d      | j                  j                  | j                  j                  }}| j                  j                  |      j                         D cg c]  \  }}|	 }	}}t        t               ||	      }
|t        |
|      z  }||t!        |
|      z  }| j                  j#                          | j%                  |      S c c}}w )N__inz`Prefetching from a limited queryset is only supported on backends that support window functions.)using)partition_byorder_byr+   )r   _dbr   query	is_slicedr   featuressupports_over_clauser   low_mark	high_markget_compilerget_order_byr   r   r   r   clear_limitsfilter)queryset
field_name	instances	predicatedbr6   r7   expr_r0   windows              r%   _filter_prefetch_querysetrD   a   s   5
|4()45I		))B~~2''<<#1  'nn55x~~7O7O) ( ; ;" ; E R R T
 TWT1D T 	 
 	*xP[22	 ;;I##%??9%%
s   <D,c                   \    e Zd ZdZd Zed        Zd Zd ZddZ	ddZ
d	 Zdd
Zd Zd Zy)ForwardManyToOneDescriptoraJ  
    Accessor to the related object on the forward side of a many-to-one or
    one-to-one (via ForwardOneToOneDescriptor subclass) relation.

    In the example::

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

    ``Child.parent`` is a ``ForwardManyToOneDescriptor`` instance.
    c                     || _         y r   )r   )r"   field_with_rels     r%   __init__z#ForwardManyToOneDescriptor.__init__   s	    #
r'   c           	      *   t        d| j                  j                  j                  j                  t
        f| j                  j                  j                  | j                  j                  j                  d| j                  j                  dd      S NRelatedObjectDoesNotExist.z.RelatedObjectDoesNotExist)r)   r*   )	typer   remote_fieldmodelDoesNotExistAttributeErrorr)   r*   namer"   s    r%   rL   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   rO   rP   _base_manager
db_managerallr"   rZ   s     r%   get_querysetz'ForwardManyToOneDescriptor.get_queryset   s5    zz&&,,::EEEERVVXXr'   Nc                     t        j                  dt        d       || j                  |      S | j                  ||g      S NzLget_prefetch_queryset() is deprecated. Use get_prefetch_querysets() instead.   )
stacklevelwarningswarnr   get_prefetch_querysetsr"   r>   r<   s      r%   get_prefetch_querysetz0ForwardManyToOneDescriptor.get_prefetch_queryset   G    $		
 ..y99**9xjAAr'   c                 b   |rt        |      dk7  rt        d      |r|d   n| j                         }|j                  |d          | j                  j
                  }| j                  j                  }|D ci c]  } ||      | }}| j                  j                  d   }| j                  j                  }	|	j                  s"t        | j                  j                        dk(  r)d|j                  z  |D ch c]  } ||      d    c}i}
nd| j                  j                         z  |i}
 |j                  di |
}|j                  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#   %s__inTFr+   )len
ValueErrorr_   
_add_hintsr   get_foreign_related_valueget_local_related_valueforeign_related_fieldsrO   hiddenrS   related_query_namer;   r2   clear_orderingmultipleset_cached_value
cache_name)r"   r>   	querysetsr<   rel_obj_attrinstance_attrinstinstances_dictrelated_fieldrO   r2   rel_objr#   s                r%   rg   z1ForwardManyToOneDescriptor.get_prefetch_querysets   s   Y1,  $-9Q<$2C2C2EYq\2zz;;

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

99!<zz.. #djj&G&G"HA"M$$%JS&T)$}T':1'=)&TE
 

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

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

        - ``self`` is the descriptor managing the ``parent`` attribute
        - ``instance`` is the ``child`` instance
        - ``cls`` is the ``Child`` class (we don't need it)
        Ndefault has no rM   )r   get_cached_valueKeyErrorru   _metaget_ancestor_linkrP   r   r   rO   rz   r{   nullrL   r(   rS   )r"   r#   clsr   	has_valueancestor_linkancestorrO   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 "": "rM   " must be a "" instance.ro   GCannot assign "%r": the current database router prevents this relation.r   )
isinstancer   rO   rP   r   concrete_modelrr   object_namerS   _stater@   r   db_for_write	__class__allow_relationr   r{   related_fieldssetattrr   getattrrz   )r"   r#   r$   rO   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&?&?"(("2"2GE8CSCS4TU '@
 	

##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   rP   rS   rT   s    r%   
__reduce__z%ForwardManyToOneDescriptor.__reduce__]  s&     ))4::??;;;r'   r   )r(   r)   r*   __doc__rI   r   rL   r   r_   ri   rg   r   r   r&   r   r+   r'   r%   rF   rF   v   sO    
$ 
 
".Y	B.
`G
1fJ;X<r'   rF   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     =H5uHs   r+   )r   rO   parent_linkget_deferred_fieldsrP   r   concrete_fieldsr   anyr   r   addingr@   superr   )	r"   r#   deferred	rel_modelr   kwargsobjr   r   s	          @r%   r   z$ForwardOneToOneDescriptor.get_objectr  s    ::""..335H 

//55I1:1P1PQ1Pemm1PQF
 =H==GMNve%5!99v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_keyrO   r   r   r   rP   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#1E$$);); 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                   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%   rI   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 rK   )	rN   r   related_modelrQ   rR   rP   r)   r*   rS   rT   s    r%   rL   z3ReverseOneToOneDescriptor.RelatedObjectDoesNotExist  sk     '\\''44nE"ll00;; LL&&33LL%%!
 	
r'   c                 8    | j                   j                  |      S r   )r   r   rV   s     r%   r   z#ReverseOneToOneDescriptor.is_cached  s    ||%%h//r'   c                 ~    | j                   j                  j                  j                  |      j	                         S rX   )r   r   r[   r\   r]   r^   s     r%   r_   z&ReverseOneToOneDescriptor.get_queryset  s/    ||))77BBBOSSUUr'   Nc                     t        j                  dt        d       || j                  |      S | j                  ||g      S ra   rd   rh   s      r%   ri   z/ReverseOneToOneDescriptor.get_prefetch_queryset  rj   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 rl   )rq   rr   r_   rs   r   r   ru   rt   rS   r;   r2   ry   r{   r|   )r"   r>   r}   r<   r~   r   r   r   r2   r   r#   s              r%   rg   z0ReverseOneToOneDescriptor.get_prefetch_querysets  s>   Y1,  $-9Q<$2C2C2EYq\2||))AA**DD@IJ	--t3	JDLL..333Y?"8??+U+ 	%%'  G%l7&;<HLL//B   LL##
 	
 Ks   Ec                 v   || S 	 | j                   j                  |      }|?| j                  |j                  j                  d| j                   j                  d      |S # t        $ r |j                  }|d}n| j                   j                  j                  |      }	  | j                  |      j                  di |}| j                   j                  j                  ||       n+# | j                   j                  j                  $ r d}Y nw xY w| j                   j                  ||       Y w xY w)a  
        Get the related instance through the reverse relation.

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

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

        Keep in mind that ``Restaurant`` holds the foreign key to ``Place``.
        Nro   r   rM   r+   )r   r   r   r   r   get_forward_related_filterr_   r   r{   r   rQ   rL   r   r(   accessor_name)r"   r#   r   r   
related_pkfilter_argss         r%   r   z!ReverseOneToOneDescriptor.__get__  s*    K
	=ll33H=G$ ?00%%..0J0JL 
 N/  	=!J!"ll00KKHUKFd///BFFUUG LL&&77J ||11>> #"G# LL))(G<	=s5   A$ $?D8$"C-'D8-%DD8DD87D8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   rM   r   r   ro   r   c              3   J   K   | ]  }t        |j                          y wr   )r   r   )r   r   r#   s     r%   r   z4ReverseOneToOneDescriptor.__set__.<locals>.<genexpr>P  s$      FE %--0Fs    #)r   r   r    r   r   rS   r   r   rr   r   r   r   r   r@   r   r   r   r   tuplerv   	enumeratelocal_related_fieldsr   r{   )r"   r#   r$   r   r   indexr   s    `     r%   r&   z!ReverseOneToOneDescriptor.__set__  s     = ll33Hd3KG" 00: !3!3!8!8$? # E4<<#=#=> NN..LL..LL..44@@  !!)%+%8%8&&&" ||&"("5"5OOh# ((9  "'( 
  !\\//FF J !*$,,*<*<*Q*Q Ruu}}j.?@ !S
 LL))(E: LL//x@r'   c                 f    t         | j                  j                  | j                  j                  ffS r   )r   r   rP   rS   rT   s    r%   r   z$ReverseOneToOneDescriptor.__reduce__a  s&    ++T\\->->???r'   r   )r(   r)   r*   r   rI   r   rL   r   r_   ri   rg   r   r&   r   r+   r'   r%   r   r     sK    

 
 
 0V	B
@+ZEAN@r'   r   c                   :    e Zd ZdZd Zed        ZddZd Zd Z	y)	ReverseManyToOneDescriptora  
    Accessor to the related objects manager on the reverse side of a
    many-to-one relation.

    In the example::

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

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

    Most of the implementation is delegated to a dynamically defined manager
    class built by ``create_forward_many_to_many_manager()`` defined below.
    c                 4    || _         |j                  | _        y r   )relr   )r"   r   s     r%   rI   z#ReverseManyToOneDescriptor.__init__v  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_clsz  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   rT   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   rI   r   r   r   r   r&   r+   r'   r%   r   r   f  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   rI   r#   r   rP   r   rS   core_filters)r"   r#   r   r   s     r%   rI   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   rP   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   rv   r   r#   r   rr   )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.
            ro   T r+   )r1   r   db_for_readrP   r#   r   r4   !interprets_empty_strings_as_nullsrs   r.   _defer_next_filterr;   r   r   rv   r   r   nonemany_to_onetarget_fieldr   r   
path_infostarget_fields_known_related_objects)r"   r<   r@   empty_strings_as_nullr   valr   
rel_obj_ids           r%   _apply_rel_filterszMcreate_reverse_many_to_one_manager.<locals>.RelatedManager._apply_rel_filters  s    SV//

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

0E0Eb0I0W0W0W $DMM<3G3GH0W"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   rO   r|   rR   r   rT   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                 `   | j                   j                  ,t        | j                   j                  j                  d      	 | j                   j
                  | j                  j                  j                     S # t        t        f$ r" t        | 1         }| j                  |      cY S w xY w)NzQ instance needs to have a primary key value before this relationship can be used.)r#   r   rr   r   r(   r	  r   rO   r|   rR   r   r   r_   r  )r"   r<   r   s     r%   r_   zGcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_queryset  s     }}' }}..77: ;N O 9}}>>JJ++66  #H- 9 7/1..x889s   6A< <.B-,B-c                     t        j                  dt        d       || j                  |      S | j                  ||g      S ra   rd   rh   s      r%   ri   zPcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_prefetch_queryset  G    MM(	 229==..y8*EEr'   c                    |rt        |      dk7  rt        d      |r|d   nt        |          }|j	                  |d          |j                  |j                  xs | j                        }| j                  j                  }| j                  j                  }|D ci c]  } ||      | }}t        || j                  j                  |      }|D ]J  }| j                  j                  |      r| ||         }	t        || j                  j                  |	       L | j                  j                  j                  }
|||d|
dfS c c}w )Nrm   rn   r   ro   F)rq   rr   r   r_   rs   r.   r1   r   ru   rt   rD   rS   r   r   rO   r|   )r"   r>   r}   r<   r~   r   r   r   r   r#   r|   r   s              r%   rg   zQcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_prefetch_querysets  s-   S^q0 #  (1y|eg6J6LH16~~hll&>dhh?H::==L JJ@@MDMNIDmD147INN04::??IVH $zz++G4-l7.CDHGTZZ__h? $ 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)Nro   c                     t        | j                        s/t        dj                  j                  j                  d|       t        | j                  j                  j                         y )N'' instance expected, got )	r   rP   r   r   r   r   r   rS   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   rP   r#   r   r   r@   rr   appendr   r[   r.   r;   updater   rS   r	   atomicsave)r"   r  objsr@   r  pksr   s   `      r%   addz>create_reverse_many_to_one_manager.<locals>.RelatedManager.add$  s)    ++-$$TZZ$--HB	= C(-zz((CJJMMR,?(0256  JJsvv&   M

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

 !''bEB#,S1
  $ CBBs   : 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.aaddK  s'     0txx0$BTBBBB   (&(c                    | j                          | j                          | j                  || j                  j                  <   t        j                  | j                  | j                        }t        | j                  |      *  di |S Nro   r+   )r   r  r#   r   rS   r   r   rP   r   r\   creater"   r   r@   RelatedManagerr   s      r%   r*  zAcreate_reverse_many_to_one_manager.<locals>.RelatedManager.createP  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.acreateY  s%     3t{{3=f====r'  c                     | j                          | j                  || j                  j                  <   t	        j
                  | j                  | j                        }t        | j                  |      &  di |S r)  )
r   r#   r   rS   r   r   rP   r   r\   get_or_creater+  s      r%   r3  zHcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_or_create^  s]     &*mmF4::??#$$TZZ$--HB)<KUfUUr'   c                 T   K    t        | j                        di | d {   S 7 wr.  r   r3  r0  s     r%   aget_or_createzIcreate_reverse_many_to_one_manager.<locals>.RelatedManager.aget_or_createf  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   rS   r   r   rP   r   r\   update_or_creater+  s      r%   r8  zKcreate_reverse_many_to_one_manager.<locals>.RelatedManager.update_or_createk  s^     &*mmF4::??#$$TZZ$--HB)<NXQWXXr'   c                 T   K    t        | j                        di | d {   S 7 wr.  r   r8  r0  s     r%   aupdate_or_createzLcreate_reverse_many_to_one_manager.<locals>.RelatedManager.aupdate_or_creates  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 rM   r  )r   r   rt   r#   setr   rP   r   r   r   ru   r!  r   rO   rQ   _clearr;   )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   r@  r%  s      r%   aremovezBcreate_reverse_many_to_one_manager.<locals>.RelatedManager.aremove  s'     7]4;;7IDIIIIr'  c                H    | j                          | j                  | |       y r   )r   r>  r"   r  s     r%   clearz@create_reverse_many_to_one_manager.<locals>.RelatedManager.clear  s    ""$D$'r'   c                V   K    t        | j                        |       d {   S 7 w)Nr  r   rF  rE  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)Nro   Fr  )update_fieldsr+   )r  r   r   rP   r#   r.   r  r   rS   r	   r  r   r  )r"   r<   r  r@   r   s        r%   r>  zAcreate_reverse_many_to_one_manager.<locals>.RelatedManager._clear  s    //1((dmmL#>>"-#HOO>tzz&=>$++"F#+C#C$?HHDJJOO3DHE $, GFFs   AC))C2F)r  rF  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)Nro   Fr  r  r  )r   r   r   r   r   r   rP   r#   r	   r  rF  r!  r=  r.   r]   r@  r  )r"   r  r  rF  r@   old_objsnew_objsr   s           r%   r=  z>create_reverse_many_to_one_manager.<locals>.RelatedManager.set  s     ;Dzz((dmmL ''bEB


- $2T2#&tzz"~'9'9';#<#%#'C"h ( 4 ( 4	 $( $X9D9 (66 CB  $*T*! CBs   4B$D44D=c                Z   K    t        | j                        |||       d {   S 7 w)N)r  r  rF  r   r=  )r"   r  r  rF  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*   rI   r   do_not_call_in_templatesr   r  r  r_   ri   rg   r!  alters_datar&  r*  r1  r3  r6  r8  r;  r   r   r@  rC  rF  rI  r>  r=  rQ  r   )r   r,  r   s   @r%   r,  r     sP   	<	0
 $( 	&	P		9"		F	S0 #' #	#J )- 	C  	O "	> #	V %)!	E &*"	Y (,$	H )-% 99>>)- ?0 "&F04 J #'G$( ( !%E+/ B "&FF "&F$( 	+4 +/u 	T  r'   r,  r   )
superclassr   r,  s    `@r%   r   r     s    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   rI   reverse)r"   r   rZ  r   s      r%   rI   zManyToManyDescriptor.__init__  s    r'   c                 .    | j                   j                  S r   )r   throughrT   s    r%   r\  zManyToManyDescriptor.through  s    
 xxr'   c                     | j                   r| j                  j                  n| j                  j                  }t	        |j
                  j                  | j                  | j                         S )N)rZ  )rZ  r   r   rP   #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 setrZ  forward)rZ  r   r   r   rS   rT   s    r%   r   z4ManyToManyDescriptor._get_set_deprecation_msg_params   sI    + LLyi9&*llDHH""
 	
 9=


 	
r'   )F)r(   r)   r*   r   rI   propertyr\  r   r   r   r   r   s   @r%   rX  rX    s:     
     
 

r'   rX  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:                  "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   rI   r#   rP   r   rx   query_field_namerS   prefetch_cache_namem2m_field_namesource_field_namem2m_reverse_field_nametarget_field_namesymmetricalr   r\  rZ  r   	get_fieldsource_fieldr   r   pk_field_namesr   r   r   rt   related_valrr   r   r   r(   )r"   r#   r   r   core_filter_keyr   r   rZ  s        r%   rI   zHcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.__init__  s:   G$DM YY
(+		(D(D(F%+.99>>(),)A)A)C&),)I)I)K&#&??  ..
(+		%+.99+G+G+I(),)I)I)K&),)A)A)C&#( ;;DL"DL $ 2 2 < <T=S=S TD $ 2 2 < <T=S=S TD "D"$D&*&7&7&F&F"(.2.C.CX]]"S5<XxGWGW5X!!/25=]]##HMM2 'G
  $00JJ8TDt'''   !4!4T5K5K!LN  {{" ?((112  #r'   c                    t        | j                  |      }t        |j                        } || j                        S r   )r   rP   r^  r   r#   )r"   r   r   r   rZ  s      r%   r   zHcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.__call__C  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*  rj  rq  r   r   _has_filtersrl  rm  )r"   removed_valsfiltersremoved_vals_filterssymmetrical_filterss        r%   _build_remove_filterszUcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._build_remove_filtersL  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   ro   Tr+   )rs   r#   r1   r.   r   _next_is_stickyr;   r   )r"   r<   s     r%   r  zRcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._apply_rel_filters`  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	  rh  rR   r   rT   s    r%   get_prefetch_cachezRcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_cachej  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
  rh  rR   r   rT   s    r%   r  zZcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._remove_prefetched_objectsp  s<    77;;D<T<TU"H- s   /2 AAc                 j    | j                         x}|S t        | 	         }| j                  |      S r   )r~  r   r_   r  )r"   cacher<   r   s      r%   r_   zLcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_querysetv  s:    0022? 7/1..x88r'   c                     t        j                  dt        d       || j                  |      S | j                  ||g      S ra   rd   rh   s      r%   ri   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                  j                  }t         |j"                     j$                  j&                  }|j)                  j*                  D ci c]-  }d|j,                  z   ||      d ||j.                        / c}      }|fdfd	d
| j0                  d
fS c c}w )Nrm   rn   r   ro   z_prefetch_related_val_%srM   )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>  sA      %
 5	 ''*@(LM" 5s   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      #6 ''aii(@*M6s   03)r   rv   )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)rq   rr   r   r_   rs   r.   r1   rD   r|  rg  r\  r   rn  rj  rP   db_tabler   r@   ops
quote_nameextrar   r   columnrh  )
r"   r>   r}   r<   
join_tableqnr  r  r  r   s
          @@r%   rg   zVcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_querysets  sl   S^q0 #  (1y|eg6J6LH16~~hll&>dhh?H0((*D,A,A9H ##--d.D.DEB00J$X[[1J**B~~
  44	 5 /ii *~r!((|"5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 )Nro   r#   rY   r   Fz
%s__isnullr+   )r   db_constraintr   r   r\  r#   r   r4   supports_foreign_keysr[   r\   rj  rq  r   rl  r;   )r"   r@   rZ   r   rw  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   rV  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)Nro   Fr  r  )r  r   r   r\  r#   r	   r  
_add_itemsrj  rl  rm  )r"   r  r  r@   s       r%   r!  zCcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.add  s    ++-$$T\\DMMJB##">****  &6	 ###DOO....  *:	 ?>>s   A#B??Cc                T   K    t        | j                        |d|i d {   S 7 w)Nr  r$  )r"   r  r  s      r%   r&  zDcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aadd  s3     0txx0(8   r'  c                 t    | j                           | j                  | j                  | j                  g|  y r   )r  _remove_itemsrj  rl  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   rB  r  s     r%   rC  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)Nro   Fr  	pre_clearsenderactionr#   rZ  rP   pk_setr.   
post_clear)r   r   r\  r#   r	   r  r   m2m_changedsendrZ  rP   r  rz  r   r_   r.   r   r;   delete)r"   r@   rw  r   s      r%   rF  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   rH  rT   s    r%   rI  zFcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aclear%  s      2tzz24444s   '%'F)rF  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)Nro   Fr  r  Tflatr   )r   r   r   r\  r#   r	   r  rF  r!  r=  r.   values_listr   r   r   rP   rt   get_prep_valuer@  r  )	r"   r  rF  r  r@   r?  rN  r   fk_vals	            r%   r=  zCcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.set*  s:    ;D$$T\\DMMJB##">JJLDHHdF5EF!

222 --::BB 3 G  "H#  *#tzz: !--GGLQO!%!2!2!A!A#!F 
 "W,#NN62$OOC0  $  DKK)DHHhJ9IJ1 ?>>s   DE&&E/c                Z   K    t        | j                        |||       d {   S 7 w)N)r  rF  r  rP  )r"   r  rF  r  s       r%   rQ  zDcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.asetL  s0     0txx09I   rR  c                    t        j                  | j                  j                  | j                        }t	        | j                  |        di |}| j                  ||       |S Nro   r  r+   )r   r   r#   r   r   r\   r*  r!  )r"   r  r   r@   new_objManyRelatedManagerr   s        r%   r*  zFcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.createS  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%   r1  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\   r3  r!  r"   r  r   r@   r   createdr  r   s         r%   r3  zMcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_or_createb  sj    $$T]]%<%<t}}UB !3T__R5HW LC
 /?@<r'   c                X   K    t        | j                        dd|i| d {   S 7 wr  r5  r  s      r%   r6  zNcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aget_or_createo  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\   r8  r!  r  s         r%   r8  zPcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.update_or_createv  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  r=  r\  r   rn  r   rP   r   r   r#   rr   r   r@   rt   r!  r   r   r  )r"   rl  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c4::.!00dmmD(  #DMM$8$8$;$;SZZ]]L 
 !- F Fs KA NI (("$57  NN9-U+#::++77> 
 NN<#>#>s#CD+ , 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   rp   r+   )r\  r   r.   r  r;   rq  
difference)r"   rj  rl  r@   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)r\  r   auto_createdr   r4   supports_ignore_conflictsrZ  rj  r   r  has_listeners)r"   r@   rj  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)Nro   z%s_idr   T)ignore_conflictsFr  pre_addr  post_addr+   )dictr   r  r   r   r\  r#   r  r   r.   bulk_createrq  r  r	   r  r   r  r  rZ  rP   )r"   rj  rl  r  r  r  r@   r  r  can_fast_addr  missing_target_idss               r%   r  zJcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._add_items  sZ    #$56F6L"$MN--.?FJ$$T\\DMMJBDHDVDV%EA "3\ --33B7CC *4 *4I %  '*; ;T=M=Ma=P '*; ;Y *4 &* D  !%!=!=!#4b*" ##">$'',,#||(!% $"jj1  -  --33B7CC *<	 *<I % . !(*; ;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   ro   Fr  
pre_remover  rp   post_remover+   )r=  r   rP   r   rt   r!  r   r   r\  r#   r	   r  r   r  r  rZ  r   r_   ru  r.   r;   r   rz  r   r  )r"   rj  rl  r  r?  r   r  r@   target_model_qsold_valsrw  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*   rI   r   rS  rz  r  r~  r  r_   ri   rg   ra  r  r  r  r!  rT  r&  r@  rC  rF  rI  r=  rQ  r*  r1  r3  r6  r8  r;  r  r  r  r  r  r   )r   r  r   rZ  rV  s   @r%   r  rd    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  rU  )rV  r   rZ  r  s   ```@r%   r^  r^    s"    D	 D	Z D	L r'   )*r   re   asgiref.syncr   django.core.exceptionsr   	django.dbr   r   r   r   r	   r  r
   r   r   r   django.db.models.functionsr   django.db.models.lookupsr   r   django.db.models.queryr   django.db.models.query_utilsr   django.db.models.utilsr   r   django.utils.deprecationr   django.utils.functionalr   r   rD   rF   r   r   r   r   rX  r^  r+   r'   r%   <module>r     s   ?B  & -  9 8 0 A + : @ = 36"3 6&*m< m<`0@ : 0@fJ@ J@Z6
 6
rwt	,
5 ,
^N	r'   