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

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

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

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

    class Parent(Model):
        pass

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

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

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

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

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

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

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

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

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

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

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

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

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

   If you're looking for ``ForwardManyToManyDescriptor`` or
   ``ReverseManyToManyDescriptor``, use ``ManyToManyDescriptor`` instead.
    N)sync_to_async)
FieldError)DEFAULT_DB_ALIASNotSupportedErrorconnectionsroutertransaction)QWindowsignals)	RowNumber)GreaterThanLessThanOrEqual)QuerySet)DeferredAttribute)
AltersDataresolve_callables)RemovedInDjango60Warning)cached_propertyc                       e Zd Zd ZdS )ForeignKeyDeferredAttributec                     |j                             | j        j                  |k    r4| j                            |          r| j                            |           ||j         | j        j        <   d S N)__dict__getfieldattname	is_cacheddelete_cached_valueselfinstancevalues      ]/var/www/html/env/lib/python3.11/site-packages/django/db/models/fields/related_descriptors.py__set__z#ForeignKeyDeferredAttribute.__set__X   so      !344==$*BVBVC
 C
= J**844405$*,---    N)__name__
__module____qualname__r%    r&   r$   r   r   W   s#        6 6 6 6 6r&   r   c                 *   t          di | d|i}| j        pt          }| j        j        rt
          |         j        j        st          d          | j        j	        | j        j
        }}d | j                            |                                          D             }t          t                      ||          }|t          ||          z  }||t!          ||          z  }| j                                         |                     |          S )N__inz`Prefetching from a limited queryset is only supported on backends that support window functions.c                     g | ]\  }}|S r*   r*   ).0expr_s      r$   
<listcomp>z-_filter_prefetch_queryset.<locals>.<listcomp>j   s)     
 
 
T1D
 
 
r&   )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dbr:   r;   r4   windows	            r$   _filter_prefetch_querysetrF   `   s&   55
((()455I		))B~ &2'< 	#1   'n5x~7O)
 
 ( ; ;" ; E E R R T T
 
 
 	*xPPP[222	 ;;;I##%%%??9%%%r&   c                   d    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dS )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                     || _         d S 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$   rN   z4ForwardManyToOneDescriptor.RelatedObjectDoesNotExist   sd    
 'Z$*7H"j.9 J$111JOOO! 
 
 	
r&   c                 6    | j                             |          S r   )r   r   r!   r"   s     r$   r   z$ForwardManyToOneDescriptor.is_cached   s    z##H---r&   c                 z    | j         j        j        j                            |                                          S N)hints)r   rQ   rR   _base_manager
db_managerallr!   r[   s     r$   get_querysetz'ForwardManyToOneDescriptor.get_queryset   s0    z&,:EEEERRVVXXXr&   Nc                     t          j        dt          d           ||                     |          S |                     ||g          S NzLget_prefetch_queryset() is deprecated. Use get_prefetch_querysets() instead.   )
stacklevelwarningswarnr   get_prefetch_querysetsr!   rB   r@   s      r$   get_prefetch_querysetz0ForwardManyToOneDescriptor.get_prefetch_queryset   Z    $		
 	
 	
 	
 ..y999**9xjAAAr&   c                    |r"t          |          dk    rt          d          |r|d         n|                                 }|                    |d                    | j        j        }| j        j        fd|D             }| j        j        d         }| j        j        }|	                                st          | j        j                  dk    rd|j
        z  fd|D             i}nd| j                                        z  |i} |j        d
i |}|j        s,|D ])}	| ||	                   }
|                    |	|
           *||d| j                                        d	fS )N   Iquerysets argument of get_prefetch_querysets() should have a length of 1.r   r"   c                 (    i | ]} |          |S r*   r*   r.   instinstance_attrs     r$   
<dictcomp>zEForwardManyToOneDescriptor.get_prefetch_querysets.<locals>.<dictcomp>   %    JJJ----tJJJr&   %s__inc                 2    h | ]} |          d          S )r   r*   rq   s     r$   	<setcomp>zDForwardManyToOneDescriptor.get_prefetch_querysets.<locals>.<setcomp>   s(    &T&T&T$}}T':':1'=&T&T&Tr&   TFr*   )len
ValueErrorr`   
_add_hintsr   get_foreign_related_valueget_local_related_valueforeign_related_fieldsrQ   	is_hiddenrU   related_query_namer?   multipleset_cached_valueget_cache_name)r!   rB   	querysetsr@   rel_obj_attrinstances_dictrelated_fieldrQ   r6   rel_objr"   rs   s              @r$   rh   z1ForwardManyToOneDescriptor.get_prefetch_querysets   s    	Y1,,   $-E9Q<<$2C2C2E2EYq\222z;
:JJJJ	JJJ
9!<z. !!## 	Ls4:+L'M'MQR'R'R$%&T&T&T&T)&T&T&TEE
 
 = = ? ??KE"8?++U++ $ 	A# A A),,w*?*?@--gx@@@@J%%''
 	
r&   c                     |                      |          }|                    | j                            |                    S Nro   )r`   r   r   get_reverse_related_filter)r!   r"   qss      r$   
get_objectz%ForwardManyToOneDescriptor.get_object   s:    11vvdj;;HEEFFFr&   c                    || S 	 | j                             |          }n# t          $ r d| j                             |          v}|r$|j                            | j         j                  nd}|rG|                    |          r2|                    |          }| j                             |d          }nd}|@|r>|                     |          }| j         j	        }|j
        s|                    ||           | j                             ||           Y nw xY w|@| j         j        s4|                     | j         j        j        d| j         j        d          |S )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 rO   )r   get_cached_valueKeyErrorr}   _metaget_ancestor_linkrR   r   r   rQ   r   r   nullrN   r'   rU   )r!   r"   clsr   	has_valueancestor_linkancestorrQ   s           r$   __get__z"ForwardManyToOneDescriptor.__get__   s    K
	;j11(;;GG 	; 	; 	;DJ$F$Fx$P$PPI 001ABBB 
  !8!8!B!B  )99(CC *55h5MM9//(33#z6 $, E 11'8DDDJ''':::::1	;4 ?4:??00#':#3#<#<#<djoooN   Ns   ! C6DDc                    |rt          || j        j        j        j        j                  sIt          d|d|j        j        d| j        j        d| j        j        j        j        j        d	          ||j	        j
        %t          j        |j        |          |j	        _
        |j	        j
        %t          j        |j        |          |j	        _
        t          j        ||          st          d|z            | j        j        }|]| j                            |d	          }||                    |d           | j        j        D ]\  }}t%          ||j        d           n;| j        j        D ].\  }}t%          ||j        t)          ||j                             /| j                            ||           ||j        s|                    ||           dS dS dS )
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 "": "rO   " must be a "" instance.ro   GCannot assign "%r": the current database router prevents this relation.r   )
isinstancer   rQ   rR   r   concrete_modelrz   object_namerU   _staterD   r   db_for_write	__class__allow_relationr   r   related_fieldssetattrr   getattrr   )r!   r"   r#   rQ   relatedlh_fieldrh_fields          r$   r%   z"ForwardManyToOneDescriptor.__set__  s4    Z4:*06E&
 &
 * EEN...JOOOJ+17CCC   !)%+%8&& & &" |&"("5Oh# # # (99   "'(  
 z.
 = j11(D1IIG
 "--gt<<<&*j&? : :"(("2D9999:
 '+j&? V V"(("2GE8CS4T4TUUUU 	
##He444
 \%:))%::::: r&   c                 @    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   rR   rU   rV   s    r$   
__reduce__z%ForwardManyToOneDescriptor.__reduce__Y  s     )4:?;;;r&   r   )r'   r(   r)   __doc__rK   r   rN   r   r`   rj   rh   r   r   r%   r   r*   r&   r$   rH   rH   u   s        
 
$ $ $ 
 
 _
". . .Y Y Y	B 	B 	B 	B+
 +
 +
 +
ZG G G
1 1 1 1fJ; J; J;X< < < < <r&   rH   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                    | j         j        j        r                                }| j         j        j        }d |j        j        D             t          fd|D                       sDfdD             } |di |}j        j	        |j        _	        j        j
        |j        _
        |S t                                                    S )Nc                     g | ]	}|j         
S r*   )r   r.   r   s     r$   r1   z8ForwardOneToOneDescriptor.get_object.<locals>.<listcomp>t  s    QQQemQQQr&   c              3       K   | ]}|v V  	d S r   r*   )r.   r   fieldss     r$   	<genexpr>z7ForwardOneToOneDescriptor.get_object.<locals>.<genexpr>y  s'      ==5u======r&   c                 2    i | ]}|t          |          S r*   )r   r.   r   r"   s     r$   rt   z8ForwardOneToOneDescriptor.get_object.<locals>.<dictcomp>z  s%    NNNe%5!9!9NNNr&   r*   )r   rQ   parent_linkget_deferred_fieldsrR   r   concrete_fieldsanyr   addingrD   superr   )r!   r"   deferred	rel_modelkwargsobjr   r   s    `    @r$   r   z$ForwardOneToOneDescriptor.get_objectn  s    :". 	3355H 
/5IQQ1PQQQF
 ====H===== NNNNvNNNi))&))$,O$:
! ( 2

ww!!(+++r&   c                 H   t                                          ||           | j        j        rn| j        j        j        r_|j        }d |j        D             }|D ]F}|j        j        j        j	        j
        }|t          ||          nd }t          |||           Cd S d S d S )Nc                 .    g | ]}|j         	|j        |S r*   )primary_keyrQ   r   s     r$   r1   z5ForwardOneToOneDescriptor.__set__.<locals>.<listcomp>  s>     # # #$# */);## # #r&   )r   r%   r   r   rQ   r   r   r   rR   pkr   r   r   )	r!   r"   r#   optsinherited_pk_fieldsr   rel_model_pk_name	raw_valuer   s	           r$   r%   z!ForwardOneToOneDescriptor.__set__  s    %((( :! 	@dj&=&I 	@>D# #!1# # #
 - @ @$)$6$<$B$E$M!9>9JGE#4555PT  "3Y????	@ 	@ 	@ 	@@ @r&   )r'   r(   r)   r   r   r%   __classcell__r   s   @r$   r   r   b  sd        	 	, , , , ,&@ @ @ @ @ @ @ @ @r&   r   c                   ^    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dS )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                     || _         d S r   )r   )r!   r   s     r$   rK   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 rM   )	rP   r   related_modelrS   rT   rR   r(   r)   rU   rV   s    r$   rN   z3ReverseOneToOneDescriptor.RelatedObjectDoesNotExist  sd     '\'4nE"l0; L&333L%%%! 
 
 	
r&   c                 6    | j                             |          S r   )r   r   rX   s     r$   r   z#ReverseOneToOneDescriptor.is_cached  s    |%%h///r&   c                 p    | j         j        j                            |                                          S rZ   )r   r   r\   r]   r^   r_   s     r$   r`   z&ReverseOneToOneDescriptor.get_queryset  s-    |)7BBBOOSSUUUr&   Nc                     t          j        dt          d           ||                     |          S |                     ||g          S rb   re   ri   s      r$   rj   z/ReverseOneToOneDescriptor.get_prefetch_queryset  rk   r&   c                   	 |r"t          |          dk    rt          d          |r|d         n|                                 }|                    |d                    | j        j        j        }| j        j        j        		fd|D             }d| j        j        j        z  |i} |j	        d	i |}|D ]3}| ||                   }| j        j        
                    ||           4||	d| j                                        dfS )
Nrm   rn   r   ro   c                 (    i | ]} |          |S r*   r*   rq   s     r$   rt   zDReverseOneToOneDescriptor.get_prefetch_querysets.<locals>.<dictcomp>  ru   r&   rv   TFr*   )ry   rz   r`   r{   r   r   r}   r|   rU   r?   r   r   )
r!   rB   r   r@   r   r   r6   r   r"   rs   s
            @r$   rh   z0ReverseOneToOneDescriptor.get_prefetch_querysets  s=    	Y1,,   $-E9Q<<$2C2C2E2EYq\222|)A*DJJJJ	JJJDL.33Y?"8?++U++   	C 	CG%ll7&;&;<HL//BBBBL''))
 	
r&   c                 ,   || S 	 | j                             |          }n# t          $ r |j        }|d}n~| j         j                            |          }	  |                     |          j        di |}| j         j                            ||           n# | j         j	        j
        $ r d}Y nw xY w| j                             ||           Y nw xY w|<|                     |j        j        d| j                                         d          |S )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   rO   r*   )r   r   r   r   r   get_forward_related_filterr`   r   r   r   rS   rN   r   r'   get_accessor_name)r!   r"   r   r   
related_pkfilter_argss         r$   r   z!ReverseOneToOneDescriptor.__get__  s`    K
	=l33H==GG 	= 	= 	=!J!"l0KKHUUKFd///BBFUUUUG L&77JJJJ |1> # # #"GGG# L))(G<<<<<	=" ?00%...0N0N0P0P0P0PR  
 Ns3   ! 5C!B8!CB2/C1B22CCc                    |\| j                             d          }|<| j                                        t          || j         j        j        d           dS dS t          || j         j                  sQt          d|dj	        j
        d| j                                         d| j         j        j	        j
        d	          j        j        %t          j        j        |          j        _        |j        j        %t          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                            |           dS )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   rO   r   r   ro   r   c              3   B   K   | ]}t          |j                  V  d S r   )r   r   r   s     r$   r   z4ReverseOneToOneDescriptor.__set__.<locals>.<genexpr>I  sC         %-00     r&   )r   r   r   r   r   rU   r   r   rz   r   r   r   r   rD   r   r   r   r   tupler~   	enumeratelocal_related_fieldsr   r   )r!   r"   r#   r   r   indexr   s    `     r$   r%   z!ReverseOneToOneDescriptor.__set__  s7     = l33Hd3KKG" 00::: !3!8$????? #" E4<#=>> )	A* EEN...L224444L.4@@@   !)%+%8&& & &" |&"("5Oh# # # (99   "'(  
     !\/F    J !*$,*<*Q R R A Auu}j.?@@@@ L))(E::: L//x@@@@@r&   c                 @    t           | j        j        | j        j        ffS r   )r   r   rR   rU   rV   s    r$   r   z$ReverseOneToOneDescriptor.__reduce__Z  s    +T\->???r&   r   )r'   r(   r)   r   rK   r   rN   r   r`   rj   rh   r   r%   r   r*   r&   r$   r   r     s        
 
  
 
 
 _
 0 0 0V V V	B 	B 	B 	B
 
 
 
:+ + + +ZEA EA EAN@ @ @ @ @r&   r   c                   B    e Zd ZdZd Zed             ZddZd Zd Z	dS )	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                 ,    || _         |j        | _        d S r   )relr   )r!   r   s     r$   rK   z#ReverseManyToOneDescriptor.__init__o  s    Y


r&   c                 X    | 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_clss  s,    .1*4H
 
 	
r&   Nc                 4    || S |                      |          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11r&   c                 8    d| j                                         fS )Nzreverse side of a related set)r   r   rV   s    r$   _get_set_deprecation_msg_paramsz:ReverseManyToOneDescriptor._get_set_deprecation_msg_params  s     +H&&((
 	
r&   c                 J    t          d|                                 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45
 
 	
r&   r   )
r'   r(   r)   r   rK   r   r   r   r   r%   r*   r&   r$   r   r   _  sz            
 
 _
2 2 2 2
 
 

 
 
 
 
r&   r   c                 8     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_        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| _        d S r   )r   rK   r"   r   rR   r   rU   core_filters)r!   r"   r   r   s     r$   rK   zCcreate_reverse_many_to_one_manager.<locals>.RelatedManager.__init__  sJ    GG$DM*DJDJ!%( ;Dr&   c                x    t          | j        |          }t          |j                  } || j                  S r   )r   rR   r   r   r"   )r!   managermanager_classr   s      r$   __call__zCcreate_reverse_many_to_one_manager.<locals>.RelatedManager.__call__  s9    dj'22G>w?PRUVVM =///r&   Tc                     | j         j        D ]<}t          | j        |j                   t          d| j        d|j         d          =d S )N"#" needs to have a value for field "z'" before this relationship can be used.)r   r~   r   r"   r   rz   )r!   r   s     r$   _check_fk_valzHcreate_reverse_many_to_one_manager.<locals>.RelatedManager._check_fk_val  sy    :  4=%-88@$SDM S S!MS S S   A r&   c                      j         pt          j         j         j                  }t
          |         j        j        }|                     j                    j         r|	                     j                   }d|_
         |j        di  j        } j        j        D ]<}t           j        |j                  }||dk    r|r|                                c S = j        j        r}	  j        j        }t           j        |j                  }n@# t(          $ r3 t+           fd j        j        d         j        D                       }Y nw xY w j        | j        ii|_        |S )X
            Filter the queryset for the instance this manager is bound to.
            ro   TN c                 D    g | ]}t          j        |j                  S r*   )r   r"   r   )r.   target_fieldr!   s     r$   r1   zacreate_reverse_many_to_one_manager.<locals>.RelatedManager._apply_rel_filters.<locals>.<listcomp>  s8        , $DM<3GHH  r&   r*   )r5   r   db_for_readrR   r"   r   r8   !interprets_empty_strings_as_nullsr{   r2   _defer_next_filterr?   r   r   r~   r   r   nonemany_to_oner  r   r   
path_infostarget_fields_known_related_objects)r!   r@   rD   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]SSSB$/%8 " 777x 4#>>$(33*.H'&x;;):;;H: + +dmU];;;3"991F9#==??***z% 
N#':#:L ")8L!M!MJJ "    "'   04
0Eb0I0W  " "JJJ JT] ;3/ Os   +D :EEc                     	 | j         j                            | j        j                                                   d S # t          t          f$ r Y d S w xY wr   )r"   _prefetched_objects_cachepopr   rQ   r   rT   r   rV   s    r$   _remove_prefetched_objectszUcreate_reverse_many_to_one_manager.<locals>.RelatedManager._remove_prefetched_objects  sk    7;;J+::<<     #H-   s   ;? AAc                 T   | j         j        !t          | j         j        j        d          	 | j         j        | j        j                                                 S # t          t          f$ r8 t                                                      }|                     |          cY S w xY w)NzQ instance needs to have a primary key value before this relationship can be used.)r"   r   rz   r   r'   r  r   rQ   r   rT   r   r   r`   r  r!   r@   r   s     r$   r`   zGcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_queryset  s     }' }.7 O O O  9}>J+::<<  #H- 9 9 9 77//11..x888889s   -A AB'&B'Nc                     t          j        dt          d           ||                     |          S |                     ||g          S rb   re   ri   s      r$   rj   zPcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_prefetch_queryset  Z    M(	    229===..y8*EEEr&   c                   	 |r"t          |          dk    rt          d          |r|d         nt                                                      }|                    |d                    |                    |j        p| j                  }| j        j        }| j        j	        		fd|D             }t          || j        j        |          }|D ]H}| j                            |          s,| ||                   }t          || j        j        |           I| j        j                                        }||	d|dfS )Nrm   rn   r   ro   c                 (    i | ]} |          |S r*   r*   rq   s     r$   rt   zecreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_prefetch_querysets.<locals>.<dictcomp>  s%    NNNDmmD114NNNr&   F)ry   rz   r   r`   r{   r2   r5   r   r}   r|   rF   rU   r   r   rQ   r   )r!   rB   r   r@   r   r   r   r"   
cache_namers   r   s            @r$   rh   zQcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_prefetch_querysets  sN    S^^q00 #   (1Ly||egg6J6J6L6LH1666~~hl&>dh??H:=L J@MNNNNINNNN04:?IVVH $ @ @z++G44 @-ll7.C.CDHGTZ_h???0??AAJ\=%URRr&   bulkc                                                                                         t          j         j         j                  } fd}|rg }|D ]U} ||           |j        j        s|j        j        |k    rt          d|z            |
                    |j                   V  j        j                            |                              |          j        di  j        j         j        i d S t%          j        |d          5  |D ]!} ||           |                                 "	 d d d            d S # 1 swxY w Y   d S )Nro   c                     t          | j                  s$t          dj        j        j        d|           t          | j        j        j                   d S )N'' instance expected, got )	r   rR   r   r   r   r   r   rU   r"   )r   r!   s    r$   check_and_update_objz\create_reverse_many_to_one_manager.<locals>.RelatedManager.add.<locals>.check_and_update_obj"  sh    !#tz22 #) !J,888C   TZ_dm<<<<<r&   zA%r instance isn't saved. Use bulk=False or save the object first.pk__inFr2   	savepointr*   )r   r  r   r   rR   r"   r   r   rD   rz   appendr   r\   r2   r?   updater   rU   r	   atomicsave)r!   r  objsrD   r$  pksr   s   `      r$   addz>create_reverse_many_to_one_manager.<locals>.RelatedManager.add  s      ++---$TZ$-HHHB	= 	= 	= 	= 	=  # ' 'C((---z( CJMR,?,?(0256   JJsv&&&&L
(..r22999EEL  
     !'bEBBB # ## # #,,S111



## # # # # # # # # # # # # # # # # #s   %EE	E	c                F   K    t          | j                  |d|i d {V S Nr  r   r/  r!   r  r-  s      r$   aaddz?create_reverse_many_to_one_manager.<locals>.RelatedManager.aaddD  s7      0tx00$BTBBBBBBBBBr&   c                     |                                   | j        || j        j        <   t	          j        | j        | j                  } t          |                     |                    j	        di |S Nro   r*   )
r   r"   r   rU   r   r   rR   r   r]   creater!   r   rD   RelatedManagerr   s      r$   r7  zAcreate_reverse_many_to_one_manager.<locals>.RelatedManager.createI  sm       &*mF4:?#$TZ$-HHHBD5)<)<==DNNvNNNr&   c                 F   K    t          | j                  di | d {V S Nr*   r   r7  r!   r   s     r$   acreatezBcreate_reverse_many_to_one_manager.<locals>.RelatedManager.acreateQ  s8      3t{33==f=========r&   c                     |                                   | j        || j        j        <   t	          j        | j        | j                  } t          |                     |                    j	        di |S r6  )
r   r"   r   rU   r   r   rR   r   r]   get_or_creater8  s      r$   r@  zHcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_or_createV  sm       &*mF4:?#$TZ$-HHHBK5)<)<==KUUfUUUr&   c                 F   K    t          | j                  di | d {V S r;  r   r@  r=  s     r$   aget_or_createzIcreate_reverse_many_to_one_manager.<locals>.RelatedManager.aget_or_create^  s9      :t'9::DDVDDDDDDDDDr&   c                     |                                   | j        || j        j        <   t	          j        | j        | j                  } t          |                     |                    j	        di |S r6  )
r   r"   r   rU   r   r   rR   r   r]   update_or_creater8  s      r$   rE  zKcreate_reverse_many_to_one_manager.<locals>.RelatedManager.update_or_createc  sn       &*mF4:?#$TZ$-HHHBN5)<)<==NXXQWXXXr&   c                 F   K    t          | j                  di | d {V S r;  r   rE  r=  s     r$   aupdate_or_createzLcreate_reverse_many_to_one_manager.<locals>.RelatedManager.aupdate_or_createk  s9      =t'<==GGGGGGGGGGGr&   c                .   |sd S |                                   | j                            | j                  }t	                      }|D ]}t          || j                  s$t          d| j        j        j	        d|          | j        
                    |          |k    r|                    |j                   t| j        j        j                            |d| j        d          |                     |                     |          |           d S )Nr"  r#  z is not related to rO   r%  )r   r   r|   r"   setr   rR   r   r   r   r}   r/  r   rQ   rS   _clearr?   )r!   r  r-  r  old_idsr   s         r$   removezAcreate_reverse_many_to_one_manager.<locals>.RelatedManager.removet  s%    F""$$$j::4=II%%  C%c4:66 'i !%
 0 < < < #   z99#>>#EECF++++"j5;HH:=##t}}}M   DKKwK77>>>>>r&   c                F   K    t          | j                  |d|i d {V S r1  r   rM  r3  s      r$   aremovezBcreate_reverse_many_to_one_manager.<locals>.RelatedManager.aremove  s7      7]4;77IDIIIIIIIIIr&   c                Z    |                                   |                     | |           d S r   )r   rK  r!   r  s     r$   clearz@create_reverse_many_to_one_manager.<locals>.RelatedManager.clear  s.    ""$$$D$'''''r&   c                N   K    t          | j                  |           d {V S )Nr  r   rS  rR  s     r$   aclearzAcreate_reverse_many_to_one_manager.<locals>.RelatedManager.aclear  s5      6]4:66DAAAAAAAAAAr&   c                    |                                   t          j        | j        | j                  }|                    |          }|r |j        di | j        j        d i d S t          j
        |d          5  |D ]>}t          || j        j        d            |                    | j        j        g           ?	 d d d            d S # 1 swxY w Y   d S )Nro   Fr'  )update_fieldsr*   )r  r   r   rR   r"   r2   r*  r   rU   r	   r+  r   r,  )r!   r@   r  rD   r   s        r$   rK  zAcreate_reverse_many_to_one_manager.<locals>.RelatedManager._clear  sG   //111(dmLLL#>>"-- F#HO>>tz&=>>>>>$+"FFF F F#+ F FC#C$???HHDJO3DHEEEEFF F F F F F F F F F F F F F F F F Fs   =ACCCF)r  rS  c                t   |                                   t          |          }| j        j        rt	          j        | j        | j                  }t          j	        |d          5  |r$| 
                    |            | j        |d|i nt          |                     |                                                    }g }|D ]1}||v r|                    |           |                    |           2 | j        |d|i  | j        |d|i d d d            d S # 1 swxY w Y   d S  | j        |d|i d S )Nro   Fr'  r  r  )r   r   r   r   r   r   rR   r"   r	   r+  rS  r/  rJ  r2   r^   rM  r)  )r!   r-  r  rS  rD   old_objsnew_objsr   s           r$   rJ  z>create_reverse_many_to_one_manager.<locals>.RelatedManager.set  s       ;;Dz +(dmLLL 'bEBBB 7 7 7


--- $2T2222#&tzz"~~'9'9';';#<#<#%#' 5 5C"h ( 4 4 4 4 ( 4 4 4 4#X9D999 (66667 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7  $*T*****s   &B+DD"%D"c                R   K    t          | j                  |||           d {V S )N)r-  r  rS  r   rJ  )r!   r-  r  rS  s       r$   asetz?create_reverse_many_to_one_manager.<locals>.RelatedManager.aset  s9      0tx00dUSSSSSSSSSSr&   r   )r'   r(   r)   rK   r   do_not_call_in_templatesr   r  r  r`   rj   rh   r/  alters_datar4  r7  r>  r@  rC  rE  rH  r   r   rM  rP  rS  rV  rK  rJ  r^  r   )r   r9  r   s   @r$   r9  r     s*       	< 	< 	< 	< 	< 	<	0 	0 	0 	0 	0
 $( 	 	 	&	 &	 &	P	 	 		9 	9 	9 	9 	9"		F 		F 		F 		F	S 	S 	S 	S 	S 	S0 #' #	# #	# #	# #	# #	#J )- 	C 	C 	C 	C 	C  	O 	O 	O 	O 	O 	O "	> 	> 	> #	V 	V 	V 	V 	V 	V %)!	E 	E 	E &*"	Y 	Y 	Y 	Y 	Y 	Y (,$	H 	H 	H )-% 9> 9	&)- ? ? ? ? ?0 "&F04 J J J J J #'G$( ( ( ( ( ( !%E+/ B B B B B "&FF F F "&F$( 	+ 	+ 	+ 	+ 	+4 +/u 	T 	T 	T 	T 	T  r&   r9  r   )
superclassr   r9  s    `@r$   r   r     sS    l  l  l  l  l  l  l  l Z l  l  l \	 r&   c                   V     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.
    Fc                 X    t                                          |           || _        d S r   )r   rK   reverse)r!   r   rf  r   s      r$   rK   zManyToManyDescriptor.__init__  s&    r&   c                     | j         j        S r   )r   throughrV   s    r$   rh  zManyToManyDescriptor.through  s    
 xr&   c                     | j         r| j        j        n| j        j        }t	          |j        j        | j        | j                   S )N)rf  )rf  r   r   rR   #create_forward_many_to_many_managerr   r   r   s     r$   r   z(ManyToManyDescriptor.related_manager_cls  sF    26,R..DHN2*4HL
 
 
 	
r&   c                 v    d| j         rdndz  | j         r| j                                        n| j        j        fS )Nz%s side of a many-to-many setrf  forward)rf  r   r   r   rU   rV   s    r$   r   z4ManyToManyDescriptor._get_set_deprecation_msg_params  sC    + L7yyi9,0LMDH&&(((djo
 	
r&   )F)r'   r(   r)   r   rK   propertyrh  r   r   r   r   r   s   @r$   rd  rd    s               
     X  
 
 _

 
 
 
 
 
 
r&   rd  c                 <     G fdd| t                     S )z
    Create a manager for the either side of a many-to-many relation.

    This manager subclasses another manager, generally the default manager of
    the related model, and adds behaviors specific to many-to-many relations.
    c                       e Zd Zd! fd	ZfdZdZd Zd Zd Z fdZ	d!d	Z
d! fd
	ZdddZde_        dddZde_        d Zde_        d Zde_         fdZde_        d Zde_        ddddZde_        ddddZde_        dd fd
Zde_        dddZde_        dd fd
Zde_        dddZde_        dd fd
Zde_        dddZde_        d Zd Zd ZdddZ fd Z xZ S )"?create_forward_many_to_many_manager.<locals>.ManyRelatedManagerNc                    t                                                       || _        sj        | _        j                                        | _        j        j        | _        j        	                                | _
        j                                        | _        j        | _        n~j        | _        j        j        | _        j                                        | _        j                                        | _
        j        	                                | _        d| _        j        | _        | _        | j        j                            | j
                  | _        | j        j                            | j                  | _        i | _        i | _        | j        j        D ]G\  }}| j        d|j        }t1          ||j                  | j        |<   |j        | j        |j        <   H| j                            |          | _        d | j        v r&t9          d|d| j        | j
                 d          |j        t9          d|j        j        z            d S )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   rK   r"   rR   r   r   query_field_namerU   prefetch_cache_namem2m_field_namesource_field_namem2m_reverse_field_nametarget_field_namesymmetricalr   rh  rf  r   	get_fieldsource_fieldr  r   pk_field_namesr   r   r   r|   related_valrz   r   r   r'   )r!   r"   r   r   core_filter_keyr   r   rf  s        r$   rK   zHcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.__init__	  s=   GG$DM ) Y
(+	(D(D(F(F%+.9>(),)A)A)C)C&),)I)I)K)K&#&?   .
(+	%+.9+G+G+I+I(),)I)I)K)K&),)A)A)C)C&#( ;DL"DL $ 2 < <T=S T TD $ 2 < <T=S T TD "D"$D&*&7&F C C"(.2.C.C.CX]]"S5<XxGW5X5X!/25=]#HM22#0JJ8TTDt''' j  xx!4T5K!L!L!LN   {" ?(12   #"r&   c                |    t          | j        |          }t          |j                  } || j                  S r   )r   rR   rj  r   r"   )r!   r   r   r   rf  s      r$   r   zHcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.__call__;  sD    dj'22G?!3 M !=$-8888r&   Tc                    t          j        | j        | j        fg          }t	          |t
                     p|                                }|r"|t          j        | j         d|fg          z  }| j        rJt          j        | j        | j        fg          }|r"|t          j        | j         d|fg          z  }||z  }|S )Nr,   )	r
   r7  rv  r}  r   r   _has_filtersrx  ry  )r!   removed_valsfiltersremoved_vals_filterssymmetrical_filterss        r$   _build_remove_filterszUcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._build_remove_filtersD  s    h!79I JKLLG |X666U,:S:S:U:U ! $ W18)?&E&E&E|%T$UVVV /&'h,d.>?@' '# ( '18!3999<HI, , ' ..Nr&   c                     |                     | j                   | j        r|                    | j                  }d|_         |                                j        di | j        S )r  ro   Tr*   )r{   r"   r5   r2   r	  _next_is_stickyr?   r   )r!   r@   s     r$   r  zRcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._apply_rel_filtersX  sj     777x 4#>>$(33*.H'48++--4IIt7HIIIr&   c                     	 | j         j                            | j                   d S # t          t
          f$ r Y d S w xY wr   )r"   r  r  rt  rT   r   rV   s    r$   r  zZcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._remove_prefetched_objectsb  sP    7;;D<TUUUUU"H-   s   $( ==c                     	 | j         j        | j                 S # t          t          f$ r8 t                                                      }|                     |          cY S w xY wr   )r"   r  rt  rT   r   r   r`   r  r  s     r$   r`   zLcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_queryseth  sj    9}>t?WXX"H- 9 9 9 77//11..x888889s    AA#"A#c                     t          j        dt          d           ||                     |          S |                     ||g          S rb   re   ri   s      r$   rj   zUcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_queryseto  r  r&   c                    |r"t          |          dk    rt          d          |r|d         nt                                                      }|                    |d                    |                    |j        p| j                  }t          |                                | j	        |          }| j
        j                            | j                  j        j        j        t           |j                 j        j        |                    fdj        D                       }|fdfdd	| j        d	fS )
Nrm   rn   r   ro   c                 ^    i | ])}d |j         z             d |j                  *S )z_prefetch_related_val_%srO   )r   column)r.   f
join_tableqns     r$   rt   zjcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_querysets.<locals>.<dictcomp>  sW         /i r*~~~~rr!(|||"5  r&   )selectc                 H     t           fdj        D                       S )Nc              3   p   K   | ]0}|                     t          d |j                             V  1dS )_prefetch_related_val_Nget_db_prep_valuer   r   )r.   r  
connectionresults     r$   r   z{create_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_querysets.<locals>.<lambda>.<locals>.<genexpr>  sd       % %
 	 ''(L(L(LMM" % % % % % %r&   )r   r   )r  r  fks   `r$   <lambda>zhcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_querysets.<locals>.<lambda>  sB    u % % % % %
  4% % %     r&   c                 H     t           fdj        D                       S )Nc              3   j   K   | ]-}|                     t          |j                            V  .d S r   r  )r.   r  r  rr   s     r$   r   z{create_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_querysets.<locals>.<lambda>.<locals>.<genexpr>  sS       # # ''ai(@(@*MM# # # # # #r&   )r   r~   )rr   r  r  s   `r$   r  zhcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_querysets.<locals>.<lambda>  s@    U # # # # #6# # #   r&   F)ry   rz   r   r`   r{   r2   r5   rF   r  rs  rh  r   rz  rv  rR   db_tabler   rD   ops
quote_nameextrar   rt  )	r!   rB   r   r@   r  r  r  r  r   s	       @@@@r$   rh   zVcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_querysetsz  s    S^^q00 #   (1Ly||egg6J6J6L6LH1666~~hl&>dh??H0((**D,A9 H #--d.DEEB0J$X[1J*B~~      4	   &  H          ( r&   through_defaultsc                J   |                                   t          j        | j        | j                  }t          j        |d          5   | j        | j        | j	        g|R d|i | j
        r | j        | j	        | j        g|R d|i d d d            d S # 1 swxY w Y   d S )Nro   Fr'  r  )r  r   r   rh  r"   r	   r+  
_add_itemsrv  rx  ry  )r!   r  r-  rD   s       r$   r/  zCcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.add  s4   ++---$T\DMJJJB#">>>  **    &6	   # #DO..    *:	                   s   A BBBc                F   K    t          | j                  |d|i d {V S )Nr  r2  )r!   r  r-  s      r$   r4  zDcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aadd  sM      0tx00(8        r&   c                 `    |                                    | j        | j        | j        g|R   d S r   )r  _remove_itemsrv  rx  r!   r-  s     r$   rM  zFcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.remove  s>    ++---Dt5t7MUPTUUUUUUr&   c                 @   K    t          | j                  |  d {V S r   rO  r  s     r$   rP  zGcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aremove  s/      3t{33T::::::::r&   c           
         t          j        | j        | j                  }t	          j        |d          5  t          j                            | j        d| j        | j	        | j
        d |           |                                  |                     t                                                                          |                    }| j        j                            |                              |                                           t          j                            | j        d| j        | j	        | j
        d |           d d d            d S # 1 swxY w Y   d S )Nro   Fr'  	pre_clearsenderactionr"   rf  rR   pk_setr2   
post_clear)r   r   rh  r"   r	   r+  r   m2m_changedsendrf  rR   r  r  r   r`   r2   r   r?   delete)r!   rD   r  r   s      r$   rS  zEcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.clear  s   $T\DMJJJB#">>>  #((<&!] L* )    //11144UWW5I5I5K5K5Q5QRT5U5UVV-33B77>>wGGNNPPP#((<'!] L* )                    s   DEEEc                 J   K    t          | j                               d {V S r   rU  rV   s    r$   rV  zFcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aclear  s0      2tz22444444444r&   F)rS  r  c                   t          |          }t          j        | j        | j                  }t          j        |d          5  |r"|                                   | j        |d|i nt          | 
                    |                              | j        j        j        d                    }g }|D ]}t          || j                  r | j                            |          d         n| j                            |          }||v r|                    |           k|                    |            | j        |   | j        |d|i d d d            d S # 1 swxY w Y   d S )Nro   Fr'  r  Tflatr   )r   r   r   rh  r"   r	   r+  rS  r/  rJ  r2   values_listr  r   r   rR   r|   get_prep_valuerM  r)  )	r!   r-  rS  r  rD   rL  r[  r   fk_vals	            r$   rJ  zCcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.set  s    ;;D$T\DMJJJB#">>> K K KJJLLLDHdF5EFFFF!

222 -:B 3   G  "H# 	1 	1  *#tz::GD-GGLLQOO!%!2!A!A#!F!F 
 "W,,#NN62222$OOC0000DK))DHhJ9IJJJ1K K K K K K K K K K K K K K K K K Ks   DEE!Ec                R   K    t          | j                  |||           d {V S )N)r-  rS  r  r]  )r!   r-  rS  r  s       r$   r^  zDcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aset  sO      0tx009I         r&   c                    t          j        | j        j        | j                  } t	          |                     |                    j        di |}|                     ||           |S Nro   r  r*   )r   r   r"   r   r   r]   r7  r/  )r!   r  r   rD   new_objManyRelatedManagerr   s        r$   r7  zFcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.create  sj    $T]%<t}UUUBKe.0C0CDDKUUfUUGHHW/?H@@@Nr&   c                J   K    t          | j                  dd|i| d {V S Nr  r*   r<  r!   r  r   s      r$   r>  zGcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.acreate&  sW      3t{33  !15;        r&   c                    t          j        | j        j        | j                  } t	          |                     |                    j        di |\  }}|r|                     ||           ||fS r  )r   r   r"   r   r   r]   r@  r/  r!   r  r   rD   r   createdr  r   s         r$   r@  zMcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_or_create-  s    $T]%<t}UUUBW5!3T__R5H5HIIW   LC
  A/?@@@<r&   c                J   K    t          | j                  dd|i| d {V S r  rB  r  s      r$   rC  zNcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aget_or_create:  sX      :t'9::  !15;        r&   c                    t          j        | j        j        | j                  } t	          |                     |                    j        di |\  }}|r|                     ||           ||fS r  )r   r   r"   r   r   r]   rE  r/  r  s         r$   rE  zPcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.update_or_createA  s    $T]%<t}UUUB5"DOOB$7$7 ) )!') )LC
  A/?@@@<r&   c                J   K    t          | j                  dd|i| d {V S r  rG  r  s      r$   rH  zQcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aupdate_or_createN  sX      =t'<==  !15;        r&   c           
         ddl m} t                      }| j        j                            |          }|D ]	}t          || j                  rt          j	        || j
                  s2t          d|d| j
        j        j        d|j        j        d          |                    |          d         }|t          d|d|d	          |                    |           t          ||          r$t!          d
| j        j        j        d|          |                    |                    |                     |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   Nz": the value for field "z	" is Noner"  r#  )django.db.modelsr  rJ  rh  r   rz  r   rR   r   r   r"   rz   r   rD   r|   r/  r   r   r  )r!   rx  r-  r  
target_idsr  r   	target_ids           r$   _get_target_idszOcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._get_target_idsU  s~    /.....J<-778IJJL E Ec4:.. E!0dmDD (j  #ssDM$8$;$;$;SZ]]]L  
 !- F Fs K KA NI ((j"ss$5$5$57   NN9----U++ E#):+777>  
 NN<#>#>s#C#CDDDDr&   c                      | j         j                            |                              |d          j        di || j        d         d|z  |i}|                    |          S )z{
            Return the subset of ids of `objs` that aren't already assigned to
            this relationship.
            Tr  r   rv   r*   )rh  r   r2   r  r?   r}  
difference)r!   rv  rx  rD   r  valss         r$   _get_missing_target_idszWcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._get_missing_target_idsu  s    -33B77.T::  *4+;A+> #44j   ((...r&   c                     | j         j        j        duot          |         j        j        }| j        p
|| j        k    o#t          j	        
                    | j                   }|||o| 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)rh  r   auto_createdr   r8   supports_ignore_conflictsrf  rv  r   r  has_listeners)r!   rD   rv  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 ! K 1T5K K!D&44T\BB  %!%?.?*? r&   c          
          |sd S t          t          pi                                          |          }t          j         j         j                  }                     |          \  }}}	|	rD j        j        	                    |          
                     fd|D             d           d S                      ||          }
t          j        |d          5  |r:t          j                             j        d j         j         j        |
|            j        j        	                    |          
                     fd	|
D             |           |r:t          j                             j        d
 j         j         j        |
|           d d d            d S # 1 swxY w Y   d S )Nro   c           
      T    g | ]$} j         di d z  j        d         d z  |i%S z%s_idr   r*   rh  r}  )r.   r  r!   rv  rx  s     r$   r1   z^create_forward_many_to_many_manager.<locals>.ManyRelatedManager._add_items.<locals>.<listcomp>  sf        & %   '*; ;T=Ma=P '*; ;Y   r&   T)ignore_conflictsFr'  pre_addr  c           
      X    g | ]&} j         di d z  j        d         d z  |i'S r  r  )r.   r  r!   rv  rx  r  s     r$   r1   z^create_forward_many_to_many_manager.<locals>.ManyRelatedManager._add_items.<locals>.<listcomp>  sp     	 	 	 & %  . !(*; ;T=Ma=P '*; ;Y 	 	 	r&   post_add)dictr   r  r   r   rh  r"   r  r   r2   bulk_creater  r	   r+  r   r  r  rf  rR   )r!   rv  rx  r  r-  r  rD   r  r  can_fast_addmissing_target_idss   ````       r$   r  zJcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._add_items  s     #$56F6L"$M$MNN--.?FFJ$T\DMJJJBDHDVDV%E EA "3\  -33B77CC      *4   &* D    !%!=!=!#4b*" " #">>> # #$ 	',,#|(!% $"j1  -    -33B77CC	 	 	 	 	 	 	 *<	 	 	 &: D    % 	',,#|)!% $"j1  -   7# # # # # # # # # # # # # # # # # #s   +B<F44F8;F8c           
         |sd S t                      }|D ]b}t          || j                  r6| j                            |          d         }|                    |           M|                    |           ct          j        | j        | j	                  }t          j        |d          5  t          j                            | j        d| j	        | j        | j        ||           t!                                                      }|                                r5 |                    |          j        d	i d| j        j        j        z  |i}	n|}	|                     |	          }
| j        j                            |                              |
                                           t          j                            | j        d| j	        | j        | j        ||           d d d            d S # 1 swxY w Y   d S )
Nr   ro   Fr'  
pre_remover  rv   post_remover*   )rJ  r   rR   r  r|   r/  r   r   rh  r"   r	   r+  r   r  r  rf  r   r`   r  r2   r?   r   r  r   r  )r!   rv  rx  r-  rL  r   r  rD   target_model_qsold_valsr  r   s              r$   r  zMcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._remove_items  sN   
   eeG % %c4:.. %!.HHMMaPFKK''''KK$$$$$T\DMJJJB#">>>  #((<'!] L*" )    #(''"6"6"8"8"//11 '?44R88?    #d&7&D&LLgV   HH  'H44X>>-33B77>>wGGNNPPP#((<(!] L*" )   +                 s   /D9G55G9<G9r   )!r'   r(   r)   rK   r   r_  r  r  r  r`   rj   rh   r/  r`  r4  rM  rP  rS  rV  rJ  r^  r7  r>  r@  rC  rE  rH  r  r  r  r  r  r   )r   r  r   rf  s   @r$   r  rp    s       0	 0	 0	 0	 0	 0	 0	 0	d	9 	9 	9 	9 	9 	9 $( 	 	 	(	J 	J 	J	 	 		9 	9 	9 	9 	9		F 		F 		F 		F0	 0	 0	 0	 0	 0	d /3 	 	 	 	 	( 59 	 	 	 	 	
  	V 	V 	V "	; 	; 	; #	 	 	 	 	4 !	5 	5 	5 "%*T 	K 	K 	K 	K 	K@ ,1D 	 	 	 	 	
  -1 	 	 	 	 	 	 	 	 "48 	 	 	 	 	
 #48 		  		  		  		  		  		  		  		  %)!;? 	 	 	 	 	
 &*"7; 		  		  		  		  		  		  		  		  (,$>B 	 	 	 	 	
 )-%	 	 	@	/ 	/ 	/&	 	 	D QUE	 E	 E	 E	 E	N/	 /	 /	 /	 /	 /	 /	 /	 /	r&   r  ra  )rb  r   rf  r  s    ``@r$   rj  rj     sZ    W W W W W W W W WZ W W Wr r&   ))r   rf   asgiref.syncr   django.core.exceptionsr   	django.dbr   r   r   r   r	   r  r
   r   r   django.db.models.functionsr   django.db.models.lookupsr   r   django.db.models.queryr   django.db.models.query_utilsr   django.db.models.utilsr   r   django.utils.deprecationr   django.utils.functionalr   r   rF   rH   r   r   r   r   rd  rj  r*   r&   r$   <module>r     s  ? ?@  & & & & & & - - - - - -              0 / / / / / / / / / 0 0 0 0 0 0 A A A A A A A A + + + + + + : : : : : : @ @ @ @ @ @ @ @ = = = = = = 3 3 3 3 3 36 6 6 6 6"3 6 6 6& & &*j< j< j< j< j< j< j< j<Z0@ 0@ 0@ 0@ 0@ : 0@ 0@ 0@fG@ G@ G@ G@ G@ G@ G@ G@T6
 6
 6
 6
 6
 6
 6
 6
rv v vr	,
 ,
 ,
 ,
 ,
5 ,
 ,
 ,
^a a a a ar&   