
    Evg                    n   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)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)Manager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      h/var/www/pixelcanvas.ch/venv/lib/python3.11/site-packages/django/db/models/fields/related_descriptors.py__set__z#ForeignKeyDeferredAttribute.__set__Y   so      !344==$*BVBVC
 C
= J**844405$*,---    N)__name__
__module____qualname__r&    r'   r%   r   r   X   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>k   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<   r5   windows	            r%   _filter_prefetch_querysetrG   a   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%   rO   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   rR   rS   _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"   rC   rA   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        }|j	        st          | j        j                  dk    rd|j
        z  fd|D             i}nd| j                                        z  |i} |j        d
i |}|j                                         |j        s,|D ])}	| ||	                   }
|                    |	|
           *||d| j        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+   rs   s     r%   	<setcomp>zDForwardManyToOneDescriptor.get_prefetch_querysets.<locals>.<setcomp>   s(    &T&T&T$}}T':':1'=&T&T&Tr'   TFr+   )len
ValueErrorrb   
_add_hintsr   get_foreign_related_valueget_local_related_valueforeign_related_fieldsrR   hiddenrV   related_query_namer@   r7   clear_orderingmultipleset_cached_value
cache_name)r"   rC   	querysetsrA   rel_obj_attrinstances_dictrelated_fieldrR   r7   rel_objr#   ru   s              @r%   rj   z1ForwardManyToOneDescriptor.get_prefetch_querysets   s    	Y1,,   $-E9Q<<$2C2C2E2EYq\222z;
:JJJJ	JJJ
9!<z.  	L#dj&G"H"HA"M"M$%&T&T&T&T)&T&T&TEE
 
 = = ? ??KE"8?++U++ 	%%''' $ 	A# A A),,w*?*?@--gx@@@@J!
 	
r'   c                     |                      |          }|                    | j                            |                    S Nrq   )rb   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 rP   )r   get_cached_valueKeyErrorr   _metaget_ancestor_linkrS   r   r   rR   r   r   nullrO   r(   rV   )r"   r#   clsr   	has_valueancestor_linkancestorrR   s           r%   __get__z"ForwardManyToOneDescriptor.__get__   s    K
	;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 "": "rP   " must be a "" instance.rq   GCannot assign "%r": the current database router prevents this relation.r   )
isinstancer   rR   rS   r   concrete_modelr|   object_namerV   _staterE   r   db_for_write	__class__allow_relationr   r   related_fieldssetattrr   getattrr   )r"   r#   r$   rR   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   rS   rV   rW   s    r%   
__reduce__z%ForwardManyToOneDescriptor.__reduce__]  s     )4:?;;;r'   r   )r(   r)   r*   __doc__rL   r   rO   r   rb   rl   rj   r   r   r&   r   r+   r'   r%   rI   rI   v   s        
 
$ $ $ 
 
 _
". . .Y Y Y	B 	B 	B 	B.
 .
 .
 .
`G G G
1 1 1 1fJ; J; J;X< < < < <r'   rI   c                   ,     e Zd ZdZ fdZ fdZ xZS )ForwardOneToOneDescriptora  
    Accessor to the related object on the forward side of a one-to-one relation.

    In the example::

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

    ``Restaurant.place`` is a ``ForwardOneToOneDescriptor`` instance.
    c                    | 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%   r2   z8ForwardOneToOneDescriptor.get_object.<locals>.<listcomp>x  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>}  s'      ==5u======r'   c                 2    i | ]}|t          |          S r+   )r   r/   r   r#   s     r%   rv   z8ForwardOneToOneDescriptor.get_object.<locals>.<dictcomp>~  s%    NNNe%5!9!9NNNr'   r+   )r   rR   parent_linkget_deferred_fieldsrS   r   concrete_fieldsanyr   addingrE   superr   )r"   r#   deferred	rel_modelkwargsobjr   r   s    `    @r%   r   z$ForwardOneToOneDescriptor.get_objectr  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_keyrR   r   s     r%   r2   z5ForwardOneToOneDescriptor.__set__.<locals>.<listcomp>  s>     # # #$# */);## # #r'   )r   r&   r   r   rR   r   r   r   rS   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   f  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%   rL   z"ReverseOneToOneDescriptor.__init__  s     r'   c           	          t          d| j        j        j        t          f| j        j        j        | j        j        j        d| j        j        dd          S rN   )	rQ   r   related_modelrT   rU   rS   r)   r*   rV   rW   s    r%   rO   z3ReverseOneToOneDescriptor.RelatedObjectDoesNotExist  sd     '\'4nE"l0; L&333L%%%! 
 
 	
r'   c                 6    | j                             |          S r   )r   r   rY   s     r%   r   z#ReverseOneToOneDescriptor.is_cached  s    |%%h///r'   c                 p    | j         j        j                            |                                          S r[   )r   r   r^   r_   r`   ra   s     r%   rb   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 rd   rg   rk   s      r%   rl   z/ReverseOneToOneDescriptor.get_prefetch_queryset  rm   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 |}|j
                                         |D ]3}| ||                   }| j        j                            ||           4||	d| j        j        dfS )
Nro   rp   r   rq   c                 (    i | ]} |          |S r+   r+   rs   s     r%   rv   zDReverseOneToOneDescriptor.get_prefetch_querysets.<locals>.<dictcomp>  rw   r'   rx   TFr+   )r{   r|   rb   r}   r   r   r   r~   rV   r@   r7   r   r   r   )
r"   rC   r   rA   r   r   r7   r   r#   ru   s
            @r%   rj   z0ReverseOneToOneDescriptor.get_prefetch_querysets  sJ    	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         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``.
        Nrq   r   rP   r+   )r   r   r   r   r   get_forward_related_filterrb   r   r   r   rT   rO   r   r(   accessor_name)r"   r#   r   r   
related_pkfilter_argss         r%   r   z!ReverseOneToOneDescriptor.__get__  sW    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%...0J0J0JL  
 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                  sDt          d|dj	        j
        d| j         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   rP   r   r   rq   r   c              3   B   K   | ]}t          |j                  V  d S r   )r   r   r   s     r%   r   z4ReverseOneToOneDescriptor.__set__.<locals>.<genexpr>P  sC         %-00     r'   )r   r   r    r   r   rV   r   r   r|   r   r   r   r   rE   r   r   r   r   tupler   	enumeratelocal_related_fieldsr   r   )r"   r#   r$   r   r   indexr   s    `     r%   r&   z!ReverseOneToOneDescriptor.__set__  s.     = l33Hd3KKG" 00::: !3!8$????? #" E4<#=>> )	A* EEN...L...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   rS   rV   rW   s    r%   r   z$ReverseOneToOneDescriptor.__reduce__a  s    +T\->???r'   r   )r(   r)   r*   r   rL   r   rO   r   rb   rl   rj   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%   rL   z#ReverseManyToOneDescriptor.__init__v  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_clsz  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                     d| j         j        fS )Nzreverse side of a related set)r   r   rW   s    r%   _get_set_deprecation_msg_paramsz:ReverseManyToOneDescriptor._get_set_deprecation_msg_params  s    +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   rL   r   r   r   r   r&   r+   r'   r%   r   r   f  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   rL   r#   r   rS   r   rV   core_filters)r"   r#   r   r   s     r%   rL   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   rS   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   r|   )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.
            rq   TN c                 D    g | ]}t          j        |j                  S r+   )r   r#   r   )r/   target_fieldr"   s     r%   r2   zacreate_reverse_many_to_one_manager.<locals>.RelatedManager._apply_rel_filters.<locals>.<listcomp>  s8        , $DM<3GHH  r'   r+   )r6   r   db_for_readrS   r#   r   r9   !interprets_empty_strings_as_nullsr}   r3   _defer_next_filterr@   r   r   r   r   r   nonemany_to_oner  r   r   
path_infostarget_fields_known_related_objects)r"   rA   rE   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        j                   d S # t          t          f$ r Y d S w xY wr   )r#   _prefetched_objects_cachepopr   rR   r   rU   r   rW   s    r%   _remove_prefetched_objectszUcreate_reverse_many_to_one_manager.<locals>.RelatedManager._remove_prefetched_objects  sb    7;;J+6     #H-   s   .2 AAc                 :   | j         j        !t          | j         j        j        d          	 | j         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   r|   r   r(   r  r   rR   r   rU   r   r   rb   r  )r"   rA   r   s     r%   rb   zGcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_queryset  s     }' }.7 O O O  9}>J+6  #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 rd   rg   rk   s      r%   rl   zPcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_prefetch_queryset  Z    M(	    229===..y8*EEEr'   c                 f  	 |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        j        }||	d|dfS )Nro   rp   r   rq   c                 (    i | ]} |          |S r+   r+   rs   s     r%   rv   zecreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_prefetch_querysets.<locals>.<dictcomp>  s%    NNNDmmD114NNNr'   F)r{   r|   r   rb   r}   r3   r6   r   r   r~   rG   rV   r   r   rR   r   )r"   rC   r   rA   r   r   r   r#   r   ru   r   s            @r%   rj   zQcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_prefetch_querysets  sE    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;J\=%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 )Nrq   c                     t          | j                  s$t          dj        j        j        d|           t          | j        j        j                   d S )N'' instance expected, got )	r   rS   r   r   r   r   r   rV   r#   )r   r"   s    r%   check_and_update_objz\create_reverse_many_to_one_manager.<locals>.RelatedManager.add.<locals>.check_and_update_obj)  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r3   	savepointr+   )r  r  r   r   rS   r#   r   r   rE   r|   appendr   r^   r3   r@   updater   rV   r	   atomicsave)r"   r   objsrE   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   r0  r"   r   r.  s      r%   aaddz?create_reverse_many_to_one_manager.<locals>.RelatedManager.aaddK  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 Nrq   r+   )r  r  r#   r   rV   r   r   rS   r   r_   creater"   r   rE   RelatedManagerr   s      r%   r8  zAcreate_reverse_many_to_one_manager.<locals>.RelatedManager.createP  s       ++---&*mF4:?#$TZ$-HHHBD5)<)<==DNNvNNNr'   c                 F   K    t          | j                  di | d {V S Nr+   r   r8  r"   r   s     r%   acreatezBcreate_reverse_many_to_one_manager.<locals>.RelatedManager.acreateY  s8      3t{33==f=========r'   c                     |                                   | j        || j        j        <   t	          j        | j        | j                  } t          |                     |                    j	        di |S r7  )
r  r#   r   rV   r   r   rS   r   r_   get_or_creater9  s      r%   rA  zHcreate_reverse_many_to_one_manager.<locals>.RelatedManager.get_or_create^  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   rA  r>  s     r%   aget_or_createzIcreate_reverse_many_to_one_manager.<locals>.RelatedManager.aget_or_createf  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 r7  )
r  r#   r   rV   r   r   rS   r   r_   update_or_creater9  s      r%   rF  zKcreate_reverse_many_to_one_manager.<locals>.RelatedManager.update_or_createk  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   rF  r>  s     r%   aupdate_or_createzLcreate_reverse_many_to_one_manager.<locals>.RelatedManager.aupdate_or_creates  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 rP   r&  )r  r   r~   r#   setr   rS   r   r   r   r   r0  r   rR   rT   _clearr@   )r"   r   r.  r  old_idsr   s         r%   removezAcreate_reverse_many_to_one_manager.<locals>.RelatedManager.remove|  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 r2  r   rN  r4  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  rL  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   rT  rS  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 )Nrq   Fr(  )update_fieldsr+   )r  r   r   rS   r#   r3   r+  r   rV   r	   r,  r   r-  )r"   rA   r   rE   r   s        r%   rL  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   rT  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 )Nrq   Fr(  r  r   )r  r   r   r   r   r   rS   r#   r	   r,  rT  r0  rK  r3   r`   rN  r*  )r"   r.  r   rT  rE   old_objsnew_objsr   s           r%   rK  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   rT  r   rK  )r"   r.  r   rT  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*   rL   r   do_not_call_in_templatesr  r  r  rb   rl   rj   r0  alters_datar5  r8  r?  rA  rD  rF  rI  r   r   rN  rQ  rT  rW  rL  rK  r_  r   )r   r:  r   s   @r%   r:  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'   r:  r   )
superclassr   r:  s    `@r%   r   r     sS    m  m  m  m  m  m  m  m Z m  m  m ^	 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   rL   reverse)r"   r   rg  r   s      r%   rL   zManyToManyDescriptor.__init__  s&    r'   c                     | j         j        S r   )r   throughrW   s    r%   ri  zManyToManyDescriptor.through  s    
 xr'   c                     | j         r| j        j        n| j        j        }t	          |j        j        | j        | j                   S )N)rg  )rg  r   r   rS   #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                 \    d| j         rdndz  | j         r| j        j        n| j        j        fS )Nz%s side of a many-to-many setrg  forward)rg  r   r   r   rV   rW   s    r%   r   z4ManyToManyDescriptor._get_set_deprecation_msg_params   s:    + L7yyi9&*lGDH""

 	
r'   )F)r(   r)   r*   r   rL   propertyri  r   r   r   r   r   s   @r%   re  re    s               
     X  
 
 _

 
 
 
 
 
 
r'   re  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                   0    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        | _        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   rL   r#   rS   r   r   query_field_namerV   prefetch_cache_namem2m_field_namesource_field_namem2m_reverse_field_nametarget_field_namesymmetricalr   ri  rg  r   	get_fieldsource_fieldr  r   pk_field_namesr   r   r   r~   related_valr|   r   r   r(   )r"   r#   r   r   core_filter_keyr   r   rg  s        r%   rL   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   rS   rk  r   r#   )r"   r   r   r   rg  s      r%   r   zHcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.__call__C  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   r8  rw  r~  r   r   _has_filtersry  rz  )r"   removed_valsfiltersremoved_vals_filterssymmetrical_filterss        r%   _build_remove_filterszUcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._build_remove_filtersL  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  rq   Tr+   )r}   r#   r6   r3   r  _next_is_stickyr@   r   )r"   rA   s     r%   r  zRcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._apply_rel_filters`  sj     777x 4#>>$(33*.H'48++--4IIt7HIIIr'   c                 b    	 | j         j        | j                 S # t          t          f$ r Y d S w xY wr   )r#   r  ru  rU   r   rW   s    r%   get_prefetch_cachezRcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_cachej  sA    }>t?WXX"H-   tts    ..c                     	 | j         j                            | j                   d S # t          t
          f$ r Y d S w xY wr   )r#   r  r  ru  rU   r   rW   s    r%   r  zZcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._remove_prefetched_objectsp  sP    7;;D<TUUUUU"H-   s   $( ==c                     |                                  x}|S t                                                      }|                     |          S r   )r  r   rb   r  )r"   cacherA   r   s      r%   rb   zLcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_querysetv  sH    00222? 77//11..x888r'   c                     t          j        dt          d           ||                     |          S |                     ||g          S rd   rg   rk   s      r%   rl   zUcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_queryset}  r  r'   c                    |r"t          |          d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 )
Nro   rp   r   rq   c                 ^    i | ])}d |j         z             d |j                  *S )z_prefetch_related_val_%srP   )r   column)r/   f
join_tableqns     r%   rv   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  rt   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   )rt   r  r  s   `r%   r  zhcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_querysets.<locals>.<lambda>  s@    U # # # # #6# # #   r'   F)r{   r|   r   rb   r}   r3   r6   rG   r  rt  ri  r   r{  rw  rS   db_tabler   rE   ops
quote_nameextrar   ru  )	r"   rC   r   rA   r  r  r  r  r   s	       @@@@r%   rj   zVcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_prefetch_querysets  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'   c                 V   | j         j        sd S t          j        | j        | j                  }t          |         j        j        sd S d| j        i}| j        j	        
                    ||          }| j        | j        j        i}| j         j        rd|d| j        z  <    |j        di |S )Nrq   r#   r\   Fz
%s__isnullr+   )r  db_constraintr   r
  ri  r#   r   r9   supports_foreign_keysr^   r_   rw  r   r   ry  r@   )r"   rE   r]   r   r  s        r%   constrained_targetzRcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.constrained_target  s    
 $2 t#DL4=IIIBr?+A t/El0;;Be;LLG-t}/?@G  % GAFt'==>!7>,,G,,,r'   c                     t           u r1|                                 | j        x}|                                S t	                                                      S r   )r
   r  r  existsr   r"   r  r   rc  s     r%   r  zFcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.exists  sU    g%%++--5+/+BB'O)00222ww~~'''r'   c                     t           u r1|                                 | j        x}|                                S t	                                                      S r   )r
   r  r  countr   r  s     r%   r  zEcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.count  sS    g%%++--5+/+BB'O)//111ww}}&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 )Nrq   Fr(  r  )r  r   r   ri  r#   r	   r,  
_add_itemsrw  ry  rz  )r"   r  r.  rE   s       r%   r0  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  r3  )r"   r  r.  s      r%   r5  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_itemsrw  ry  r"   r.  s     r%   rN  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   rP  r  s     r%   rQ  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 )Nrq   Fr(  	pre_clearsenderactionr#   rg  rS   pk_setr3   
post_clear)r   r   ri  r#   r	   r,  r   m2m_changedsendrg  rS   r  r  r   rb   r3   r   r@   delete)r"   rE   r  r   s      r%   rT  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   rV  rW   s    r%   rW  zFcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aclear%  s0      2tz22444444444r'   F)rT  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 )Nrq   Fr(  r  Tflatr   )r   r   r   ri  r#   r	   r,  rT  r0  rK  r3   values_listr  r   r   rS   r~   get_prep_valuerN  r*  )	r"   r.  rT  r  rE   rM  r\  r   fk_vals	            r%   rK  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.  rT  r  r^  )r"   r.  rT  r  s       r%   r_  zDcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.asetL  sO      0tx009I         r'   c                    t          j        | j        j        | j                  } t	          |                     |                    j        di |}|                     ||           |S Nrq   r  r+   )r   r   r#   r   r   r_   r8  r0  )r"   r  r   rE   new_objManyRelatedManagerr   s        r%   r8  zFcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.createS  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_   rA  r0  r"   r  r   rE   r   createdr  r   s         r%   rA  zMcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.get_or_createb  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  rC  r  s      r%   rD  zNcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aget_or_createo  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_   rF  r0  r  s         r%   rF  zPcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.update_or_createv  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  rH  r  s      r%   rI  zQcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager.aupdate_or_create  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  rK  ri  r   r{  r   rS   r   r   r#   r|   r   rE   r~   r0  r   r   r  )r"   ry  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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   rx   r+   )ri  r   r3   r  r@   r~  
difference)r"   rw  ry  rE   r  valss         r%   _get_missing_target_idszWcreate_forward_many_to_many_manager.<locals>.ManyRelatedManager._get_missing_target_ids  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)ri  r   auto_createdr   r9   supports_ignore_conflictsrg  rw  r   r  has_listeners)r"   rE   rw  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 )Nrq   c           
      T    g | ]$} j         di d z  j        d         d z  |i%S z%s_idr   r+   ri  r~  )r/   r  r"   rw  ry  s     r%   r2   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"   rw  ry  r  s     r%   r2   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   ri  r#   r  r   r3   bulk_creater  r	   r,  r   r  r  rg  rS   )r"   rw  ry  r  r.  r  rE   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   rq   Fr(  
pre_remover  rx   post_remover+   )rK  r   rS   r  r~   r0  r   r   ri  r#   r	   r,  r   r  r  rg  r   rb   r  r3   r@   r   r  r   r  )r"   rw  ry  r.  rM  r   r  rE   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*   rL   r   r`  r  r  r  r  rb   rl   rj   rn  r  r  r  r0  ra  r5  rN  rQ  rT  rW  rK  r_  r8  r?  rA  rD  rF  rI  r  r  r  r  r  r   )r   r  r   rg  rc  s   @r%   r  rq    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  rb  )rc  r   rg  r  s   ```@r%   rk  rk    sa    D	 D	 D	 D	 D	 D	 D	 D	 D	 D	Z D	 D	 D	L r'   )*r   rh   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   rG   rI   r   r   r   r   re  rk  r+   r'   r%   <module>r     s  ? ?B  & & & & & & - - - - - -              9 8 8 8 8 8 8 8 8 8 8 8 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& & &*m< m< m< m< m< m< m< m<`0@ 0@ 0@ 0@ 0@ : 0@ 0@ 0@fJ@ J@ J@ J@ J@ J@ J@ J@Z6
 6
 6
 6
 6
 6
 6
 6
rw w wt	,
 ,
 ,
 ,
 ,
5 ,
 ,
 ,
^N	 N	 N	 N	 N	r'   