
    0~hQ                        d Z ddl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mZ ddlmZmZ ddl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Z#dZ$d6dZ%d6dZ&d Z'	 	 	 	 	 	 	 	 	 d7ddddZ( G d d      Z) G d de      Z* G d dee	      Z+ G d de+e*      Z,e,dddddddddf
d Z- G d! d"ee	      Z.e,de.d#d$d$ddddd$dddddd$ddddd$fd%Z/ G d& d'e.      Z0d8d(Z1e,e0dddd)d$ddddd$dddddd$ddddd$fd*Z2 G d+ d,e      Z3 G d- d.      Z4 G d/ d0e      Z5 G d1 d2e      Z6 G d3 d4e6      Z7d5 Z8y)9z[
Helper functions for creating Form classes from Django models
and database field objects.
    )chain)NON_FIELD_ERRORS
FieldErrorImproperlyConfiguredValidationError)
AltersData)ChoiceFieldField)BaseFormDeclarativeFieldsMetaclass)BaseFormSetformset_factory)	ErrorList)HiddenInputMultipleHiddenInputRadioSelectSelectMultiple)BaseChoiceIterator)capfirstget_text_listgettext)gettext_lazy)	ModelFormBaseModelFormmodel_to_dictfields_for_modelModelChoiceFieldModelMultipleChoiceField
ALL_FIELDSBaseModelFormSetmodelformset_factoryBaseInlineFormSetinlineformset_factorymodelform_factory__all__Nc                 P   ddl m} |j                  }| j                  }g }|j                  D ]O  }|j
                  r$t        ||j                        s|j                  |vr5||j                  |vrF|r|j                  |v rW|j                         r| |j                     j                  j                  j                  | j                  | j                  | j                  |j                              r@|j!                  |j                        | |j                     j                  j"                  v rt        ||j$                        r|j'                  |       1|j)                  |||j                            R |D ]!  }|j)                  |||j                            # |S )z
    Construct and return a model instance from the bound ``form``'s
    ``cleaned_data``, but do not save the returned instance to the database.
    r   )models)	django.dbr(   _metacleaned_datafieldseditable
isinstance	AutoFieldnamehas_defaultfieldwidgetvalue_omitted_from_datadatafiles
add_prefixgetempty_values	FileFieldappendsave_form_data)	forminstancer,   excluder(   optsr+   file_field_listfs	            \/var/www/peopleoo.sandbox-dev.co.uk/venv/lib/python3.12/site-packages/django/forms/models.pyconstruct_instancerD   /   sa   
 !>>D$$LO[[ =

!V--.vv\)!&&"6qvv( MMOQVV""))AA		4::tqvv'>   (DL,>,>,K,KK a))*""1%X|AFF';<5=8  9	<#789 O    c                    | j                   }i }t        |j                  |j                  |j                        D ]P  }t        |dd      s||j                  |vr"|r|j                  |v r3|j                  |       ||j                  <   R |S )a  
    Return a dict containing the data in ``instance`` suitable for passing as
    a Form's ``initial`` keyword argument.

    ``fields`` is an optional list of field names. If provided, return only the
    named.

    ``exclude`` is an optional list of field names. If provided, exclude the
    named from the returned dict, even if they are listed in the ``fields``
    argument.
    r-   F)r*   r   concrete_fieldsprivate_fieldsmany_to_manygetattrr0   value_from_object)r>   r,   r?   r@   r5   rB   s         rC   r   r   _   s     >>DD4'')<)<d>O>OP 5q*e,!&&"6qvv(**84QVV5 KrE   c                 h   ddl m}m}m} t	        | d      rt	        | d      r| j                         }|r||}t        ||      s |di |}| | |d            z  }| j                  j                   || j                  j                  j                  j                  |                  | _        yyyy)	z=Apply limit_choices_to to the formfield's queryset if needed.r   )ExistsOuterRefQquerysetget_limit_choices_topk)rR   N )django.db.modelsrM   rN   rO   hasattrrQ   r.   rP   filtermodel_base_manager)	formfieldrM   rN   rO   limit_choices_tocomplex_filters         rC   #apply_limit_choices_to_to_formfieldr\   x   s    44y*%')=S*T$99;-Nna0!"!6%5!6a8D>22N!*!3!3!:!:y))//==DD^TU"I  +U%rE   Tapply_limit_choices_toform_declared_fieldsc
                
   |xs i }i }g }| j                   }ddlm} |j                  D cg c]  }t	        ||      s| }}t        t        |j                  ||j                              D ]  }t        |dd      sG|D|j                  |v r6||j                  |vr&t        d|j                  d| j                  d      X||j                  |vri|r|j                  |v rz|j                  |v r||j                     ||j                  <   i }|r |j                  |v r||j                     |d<   |t        k(  s|r|j                  |v rd	|d
<   |r |j                  |v r||j                     |d<   |r |j                  |v r||j                     |d<   |r |j                  |v r||j                     |d<   |	r |j                  |	v r|	|j                     |d<   | |j                  di |}nt        |      st!        d       ||fi |}|r|
rt#        |       |||j                  <   |j%                  |j                          |r)|D ci c]  }|r||vr||vr||j'                  |        }}|S c c}w c c}w )a  
    Return a dictionary containing form fields for the given model.

    ``fields`` is an optional list of field names. If provided, return only the
    named fields.

    ``exclude`` is an optional list of field names. If provided, exclude the
    named fields from the returned fields, even if they are listed in the
    ``fields`` argument.

    ``widgets`` is a dictionary of model field names mapped to a widget.

    ``formfield_callback`` is a callable that takes a model field and returns
    a form field.

    ``localized_fields`` is a list of names of fields which should be localized.

    ``labels`` is a dictionary of model field names mapped to a label.

    ``help_texts`` is a dictionary of model field names mapped to a help text.

    ``error_messages`` is a dictionary of model field names mapped to a
    dictionary of error messages.

    ``field_classes`` is a dictionary of model field names mapped to a form
    field class.

    ``apply_limit_choices_to`` is a boolean indicating if limit_choices_to
    should be applied to a field's queryset.

    ``form_declared_fields`` is a dictionary of form fields created directly on
    a form.
    r   )r
   r-   F'z' cannot be specified for z) model form as it is a non-editable fieldr3   Tlocalizelabel	help_texterror_messages
form_classz1formfield_callback must be a function or callablerS   )r*   rT   r
   rH   r.   sortedr   rG   rI   rJ   r0   r   __name__r    rY   callable	TypeErrorr\   r;   r8   )rW   r,   r?   widgetsformfield_callbacklocalized_fieldslabels
help_textsre   field_classesr^   r_   
field_dictignoredr@   
ModelFieldrB   sortable_private_fieldskwargsrY   s                       rC   r   r      s   ` 052JG;;D4 &&*Q
*C  d""$;T=N=NO 2# q*e,"FFf$_g(= ,-FFENND  !&&"6qvv(66))!5aff!=Jqvvqvv(&qvvF8z)+;!;!%F:aff&$QVVnF7O!&&J.",QVV"4F;aff6'5aff'=F#$QVV}4#0#8F< %#-f-I,-OPP*177I%3I>!*JqvvNN166"e2#f  
q/Qg5E z~~a  

 

 yn
s   I;I;#J c                       e Zd ZddZy)ModelFormOptionsNc                 l   t        |dd       | _        t        |dd       | _        t        |dd       | _        t        |dd       | _        t        |dd       | _        t        |dd       | _        t        |dd       | _        t        |dd       | _        t        |d	d       | _	        t        |d
d       | _
        y )NrW   r,   r?   rk   rm   rn   ro   re   rp   rl   )rJ   rW   r,   r?   rk   rm   rn   ro   re   rp   rl   )selfoptionss     rC   __init__zModelFormOptions.__init__   s    Wgt4
gx6w	48w	48 '1CT Jgx6!'<>%g/?F$WotD")'3G"NrE   N)rh   
__module____qualname__r{   rS   rE   rC   rw   rw      s    
OrE   rw   c                        e Zd Z fdZ xZS )ModelFormMetaclassc                    t         |   | |||      }|t        fk(  r|S t        t	        |dd             x}|_        dD ]F  }t	        ||      }t        |t              s |t        k7  s*d|j                  ||dz  }t        |       |j                  rZ|j                  |j                  t        d|z        |j                  t        k(  rd |_        t        |j                  |j                  |j                  |j                   |j"                  |j$                  |j&                  |j(                  |j*                  |j,                  d|j.                        }	|	j1                         D 
ch c]
  \  }
}|r	|
 }}
}|j3                  |j.                        }|r7d}|d	j5                  |      |j                  j                  fz  }t7        |      |	j9                  |j.                         n|j.                  }	|	|_        |S c c}}
w )
NMeta)r,   r?   rm   zP%(model)s.Meta.%(opt)s cannot be a string. Did you mean to type: ('%(value)s',)?)rW   optvaluez|Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form %s needs updating.Fr]   z&Unknown field(s) (%s) specified for %sz, )super__new__r   rw   rJ   r*   r.   strr    rh   rj   rW   r,   r?   r   r   rk   rl   rm   rn   ro   re   rp   declared_fieldsitems
differencejoinr   updatebase_fields)mcsr0   basesattrs	new_classr@   r   r   msgr,   kvnone_model_fieldsmissing_fieldsmessage	__class__s                  rC   r   zModelFormMetaclass.__new__  s   GOCue<	]$$!1')VT2R!SSy
 = 	%CD#&E%%%:*=< "+!3!3"!&   n$	% ::{{"t||';*&(,-  {{j( #%

''%%##""',%.%>%>F" 06||~ Gtq!Q G G.99):S:STNBDIIn5tzz7J7JKK ))MM)334..F &	 !Hs   -
H8H)rh   r}   r~   r   __classcell__r   s   @rC   r   r     s    E ErE   r   c                   p     e Zd Zdddddedddddf fd	Zd Zd Zd Zd Zd	 Z	d
 Z
ddZde_         xZS )r   Nid_%sFc                    | j                   }|j                  t        d      |	|j                         | _        i }n(|	| _        t	        |	|j
                  |j                        }||j                  |       d| _        t        | )  |||||||||
|
       | j
                  j                         D ]  }t        |        y )Nz'ModelForm has no model class specified.F)use_required_attributerenderer)r*   rW   
ValueErrorr>   r   r,   r?   r   _validate_uniquer   r{   valuesr\   )ry   r5   r6   auto_idprefixinitialerror_classlabel_suffixempty_permittedr>   r   r   r@   object_datarY   r   s                  rC   r{   zBaseModelForm.__init__W  s     zz::FGG JJLDMK$DM'$++t||LKw' !&#9 	 	
 ++- 	;I/	:	;rE   c                 2   t               }| j                  j                  j                  D ]h  }|j                  }|| j                  vr|j                  |j                         :| j                  j                  r4|| j                  j                  vr|j                  |j                         | j                  j                  r4|| j                  j                  v r|j                  |j                         || j                  v r|j                  |j                         | j                  |   }| j                  j                  |      }|j                  r0|j                  r>||j                  v sN|j                  |j                         k |S )z
        For backwards-compatibility, exclude several types of fields from model
        validation. See tickets #12507, #12521, #12553.
        )setr>   r*   r,   r0   addr?   _errorsr+   r8   blankrequiredr9   )ry   r?   rB   r2   
form_fieldfield_values         rC   _get_validation_exclusionsz(BaseModelForm._get_validation_exclusions  s,   
 % $$++ "	(AFFE DKK'AFF#
 ""uDJJ4E4E'EAFF###1C1C(CAFF# $,,&AFF# "[[/
"//33E:&//#z'>'>>KK'E"	(F rE   c                 (    d| _         | j                  S )NT)r   r+   ry   s    rC   cleanzBaseModelForm.clean  s     $   rE   c                    | j                   }t        |d      r|j                  }nt        |i}|j	                         D ]  \  }}|t        k(  r2|j
                  r&t        |j
                  v r|j
                  t           }n)|| j                  v r| j                  |   j
                  }nj|D ]6  }t        |t              s|j                  |v s#||j                     |_
        8  | j                  d |       y )N
error_dict)r*   rU   r   r   r   re   r,   r.   r   coder   	add_error)ry   errorsr@   r   r2   messagesre   r   s           rC   _update_errorszBaseModelForm._update_errors  s     zz 6<(**J*F3J)//1 	COE8))''$(;(;;!%!4!45E!F$++%!%U!3!B!B# Cw86&4W\\&BGOC	C& 	tV$rE   c                 :   | j                   }| j                         }| j                  j                         D ]'  \  }}t	        |t
              s|j                  |       ) 	 t        | | j                  |j                  |j                        | _        	 | j                  j                  |d       | j                  r| j                          y y # t        $ r}| j                  |       Y d }~[d }~ww xY w# t        $ r}| j                  |       Y d }~dd }~ww xY w)NF)r?   validate_unique)r*   r   r,   r   r.   InlineForeignKeyFieldr   rD   r>   r?   r   r   
full_cleanr   r   )ry   r@   r?   r0   r2   es         rC   _post_cleanzBaseModelForm._post_clean  s    zz113  ;;,,. 	"KD%%!67D!	"	#.dmmT[[$,,DM	#MM$$We$L
     " !  	#""	#
  	#""	#s0   "1C C6 	C3C..C36	D?DDc                     | j                         }	 | j                  j                  |       y# t        $ r}| j	                  |       Y d}~yd}~ww xY w)z
        Call the instance's validate_unique() method and update the form's
        validation errors if any were raised.
        )r?   N)r   r>   r   r   r   )ry   r?   r   s      rC   r   zBaseModelForm.validate_unique  sM    
 113	#MM))'): 	#""	#s   / 	AAAc                    | j                   }| j                  j                  }| j                  j                  }| j                  j                  }t        |j                  |j                        D ]i  }t        |d      s|r|j                  |vr!|r|j                  |v r2|j                  |v sA|j                  | j                  ||j                            k y)zS
        Save the many-to-many fields and generic relations for this form.
        r<   N)r+   r*   r?   r,   r>   r   rI   rH   rU   r0   r<   )ry   r+   r?   r,   r@   rB   s         rC   	_save_m2mzBaseModelForm._save_m2m  s     ((**$$""}}"" t(($*=*=> 	FA1./!&&.166W,vv%  QVV0DE	FrE   Tc                 h   | j                   rRt        d| j                  j                  j                  d| j                  j
                  j                  rdndd      |r6| j                  j                          | j                          | j                  S | j                  | _	        | j                  S )z
        Save this form's self.instance object if commit=True. Otherwise, add
        a save_m2m() method to the form which can be called after the instance
        is saved manually at a later time. Return the model instance.
        zThe z could not be createdchangedz" because the data didn't validate.)
r   r   r>   r*   object_name_stateaddingsaver   save_m2m)ry   commits     rC   r   zBaseModelForm.save  s     ;; MM''33!%!5!5!<!<I)K  MM NN
 }} !NNDM}}rE   T)rh   r}   r~   r   r{   r   r   r   r   r   r   r   alters_datar   r   s   @rC   r   r   V  s`     #,;\+Z!%B#@	#F*0 DrE   r   c                       e Zd Zy)r   N)rh   r}   r~   rS   rE   rC   r   r   2  s    rE   r   )	metaclassc                    d| i}|||d<   |||d<   |||d<   |||d<   |||d<   |||d<   |	|	|d	<   |
|
|d
<   t        |d      r|j                  fnd}t        d||      }|rt        |      |_        | j
                  dz   }d|i}t        |dd      t        |dd      t        d       t        |      ||f|      S )al  
    Return a ModelForm containing form fields for the given model. You can
    optionally pass a `form` argument to use as a starting point for
    constructing the ModelForm.

    ``fields`` is an optional list of field names. If provided, include only
    the named fields in the returned fields. If omitted or '__all__', use all
    fields.

    ``exclude`` is an optional list of field names. If provided, exclude the
    named fields from the returned fields, even if they are listed in the
    ``fields`` argument.

    ``widgets`` is a dictionary of model field names mapped to a widget.

    ``localized_fields`` is a list of names of fields which should be localized.

    ``formfield_callback`` is a callable that takes a model field and returns
    a form field.

    ``labels`` is a dictionary of model field names mapped to a label.

    ``help_texts`` is a dictionary of model field names mapped to a help text.

    ``error_messages`` is a dictionary of model field names mapped to a
    dictionary of error messages.

    ``field_classes`` is a dictionary of model field names mapped to a form
    field class.
    rW   Nr,   r?   rk   rm   rn   ro   re   rp   r   rS   FormzZCalling modelform_factory without defining 'fields' or 'exclude' explicitly is prohibited.)rU   r   typestaticmethodrl   rh   rJ   r   )rW   r=   r,   r?   rl   rk   rm   rn   ro   re   rp   r   r   r   
class_nameform_class_attrss                   rC   r%   r%   6  s)   ` eE h"i"i#$4 ! h(l!"0 !.o $D&1TYYLrEu%D"./A"B&(J ~tXt$,y$1O1W"2
 	
 4:j4'+;<<rE   c                        e Zd ZdZdZdZ e       Z	 	 	 	 	 ddd fdZ fdZ	d Z
d Z fd	Zd
 ZddZddZddZddZde_        d Zd Zd Zd Zd ZddZddZ fdZ xZS )r!   zO
    A ``FormSet`` for editing a queryset and/or adding new objects to it.
    NF)r   c          	      N    || _         || _        t        |   di ||||d| y )N)r5   r6   r   r   rS   )rP   initial_extrar   r{   )	ry   r5   r6   r   r   rP   r   ru   r   s	           rC   r{   zBaseModelFormSet.__init__  sA     !$ 	
" 	
 	
rE   c                 j    | j                   st        | j                               S t        |          S )z=Return the number of forms that are required in this FormSet.)is_boundlenget_querysetr   initial_form_country   r   s    rC   r   z#BaseModelFormSet.initial_form_count  s,    }}t((*++w)++rE   c                     t        | d      s.| j                         D ci c]  }|j                  | c}| _        | j                  j	                  |      S c c}w )N_object_dict)rU   r   rR   r   r8   )ry   rR   os      rC   _existing_objectz!BaseModelFormSet._existing_object  sM    t^,262C2C2E FQq FD  $$R(( !Gs   Ac                     |j                   '|j                   j                         }|j                   '|j                  S )z
        If the field is a related field, fetch the concrete field's (that
        is, the ultimate pointed-to field's) to_python.
        )remote_fieldget_related_field	to_pythonry   r2   s     rC   _get_to_pythonzBaseModelFormSet._get_to_python  s<    
   ,&&88:E   ,rE   c                    || j                         k  }|r| j                  r| j                  |      d| j                  j                  j
                  j                  }	 | j                  |   }| j                  | j                  j                  j
                        }	  ||      }| j                  |      |d<   nG| j                         |   |d<   n0| j                  r$	 | j                  || j                         z
     |d<   t        | @  |fi |}|r<d|j"                  | j                  j                  j
                  j                     _        |S # t        $ r Y [w xY w# t        $ r Y jw xY w# t        $ r Y yw xY w)N-r>   r   T)r   r   r7   rW   r*   rR   r0   r5   r   r   r   KeyErrorr   r   
IndexErrorr   _construct_formr,   r   )	ry   iru   pk_requiredpk_keyrR   r   r=   r   s	           rC   r   z BaseModelFormSet._construct_form  sq   $1133}}$(OOA$6

8H8H8K8K8P8PQG6*B !% 3 3DJJ4D4D4G4G HIG&r] .2-B-B2-Fz*%)%6%6%8%;z"$($6$6q4;R;R;T7T$Uy! w&q3F3=ADKK

((++001:# +      (  s6   "E$ !E "#E3 	E! E!$	E0/E03	E?>E?c                 D   t        | d      s| j                  | j                  }n$| j                  j                  j	                         }|j
                  s9|j                  | j                  j                  j                  j                        }|| _
        | j                  S )N	_queryset)rU   rP   rW   _default_managerr   orderedorder_byr*   rR   r0   r   )ry   qss     rC   r   zBaseModelFormSet.get_queryset  sw    t[)}}(]]ZZ00==?
 ::[[!1!1!4!4!9!9:
  DN~~rE   Tc                 &    |j                  |      S )z8Save and return a new model instance for the given form.r   r   )ry   r=   r   s      rC   save_newzBaseModelFormSet.save_new      yyy''rE   c                 &    |j                  |      S )z>Save and return an existing model instance for the given form.r   r   )ry   r=   objr   s       rC   save_existingzBaseModelFormSet.save_existing   r   rE   c                 *    |r|j                          yy)z#Deletes an existing model instance.N)delete)ry   r   r   s      rC   delete_existingz BaseModelFormSet.delete_existing  s    JJL rE   c                      |sg  _          fd}| _         j                  r j                  |      S  j                  |       j	                  |      z   S )z
        Save model instances for every form, adding and changing instances
        as necessary, and return the list of instances.
        c                  H    j                   D ]  } | j                           y r|   )saved_formsr   )r=   ry   s    rC   r   z'BaseModelFormSet.save.<locals>.save_m2m  s!     ,, $DMMO$rE   )r  r   	edit_onlysave_existing_objectssave_new_objects)ry   r   r   s   `  rC   r   zBaseModelFormSet.save	  sW    
 !D$ %DM>>--f55--f58M8Mf8UUUrE   c                 $    | j                          y r|   )r   r   s    rC   r   zBaseModelFormSet.clean  s    rE   c                     t               }t               } j                  } j                  D cg c]  }|j                         r||vr| }}|D ]T  j	                         }j
                  j                  |d      \  }}|j                  |       |j                  |       V g }	|D ]  \  }
}t               }|D ]   fd|D        }t        d |D              }|s$d |vs)||v r|	j                   j                  |              j                   j                         g j                        j                  t        <   |D ]  }|j                   v sj                   |=   |j#                  |         |D ].  }t               }|\  }
}}}|D ]  j                   sj                   |   !j                   |   1|dk(  r3j                   |   }|j$                  |j&                  |j(                  f}nt+        j                   |   |      f}j                   |   f|z   }||v rj|	j                   j-                  |              j                   j                         g j                        j                  t        <   j                   |= |j#                  |        1 |	rt/        |	      y c c}w )NT)r?   include_meta_constraintsc              3   x   K   | ]1  }|j                   v r!|j                  v r|nj                   |    3 y wr|   )r+   unique_fields).0r2   r=   ry   s     rC   	<genexpr>z3BaseModelFormSet.validate_unique.<locals>.<genexpr>:  sE       1 11 #d&8&88Ed>O>OPU>VVs   7:c              3      K   | ]=  }t        |d       r|j                         nt        |t              rt	        |      n| ? yw)_get_pk_valN)rU   r  r.   listtuple)r  ds     rC   r  z3BaseModelFormSet.validate_unique.<locals>.<genexpr>@  sA      !  (/q-'@AMMO%/4%8qa@!s   AA)r   date)r   deleted_formsformsis_validr   r>   _get_unique_checksr   r  r;   get_unique_error_messager   get_form_errorr   r   r   r+   r   yearmonthdayrJ   get_date_error_messager   )ry   all_unique_checksall_date_checksforms_to_deleter=   valid_formsr?   unique_checksdate_checksr   uclassunique_check	seen_datarow_datar2   
date_checklookup
unique_forr  	date_datar5   s   `   `                rC   r   z BaseModelFormSet.validate_unique   s   E%,, 


}}4#> 
 

   	0D557G)-)I)I)- *J *&M; $$]3"";/	0 $5 !	, FLI# ,!- ! ! &	!  H 49, d&C&CL&QR9=9I9I!0023%)]] :J :%56 &2 =E$(9(99$($5$5e$<= MM(+?,!	,F *  	(JI0:-FFE:# ( %%))%0<))*5A '#00<%)YY

DHH$E	 &-T->->z-JF%S$U	 --e46BDy( d&A&A*&MN9=9I9I!0023%)]] :J :%56 !--e4MM$';( 	(D !&)) k
s   Kc                     t        |      dk(  rt        d      d|d   iz  S t        d      dt        |t        d            iz  S )N   z0Please correct the duplicate data for %(field)s.r2   r   zFPlease correct the duplicate data for %(field)s, which must be unique.and)r   r   r   _)ry   r)  s     rC   r  z)BaseModelFormSet.get_unique_error_message}  s_    |!MNaR   X |QuX> rE   c                 J    t        d      |d   |d   t        |d         dz  S )NzoPlease correct the duplicate data for %(field_name)s which must be unique for the %(lookup)s in %(date_field)s.      r1  )
field_name
date_fieldr-  )r   r   )ry   r,  s     rC   r!  z'BaseModelFormSet.get_date_error_message  s;    I
 %Q-$Q-*Q-(

 	
rE   c                     t        d      S )Nz*Please correct the duplicate values below.r   r   s    rC   r  zBaseModelFormSet.get_form_error  s    CDDrE   c                    g | _         g | _        | j                  sg S g }| j                  }| j                  D ]  }|j                  }|j
                  ||v r/| j                  j                  |       | j                  ||       O|j                         s`| j                   j                  ||j                  f       |j                  | j                  |||             |r| j                  j                  |        |S Nr   )changed_objectsdeleted_objectsinitial_formsr  r>   rR   r;   r  has_changedchanged_datar  r  )ry   r   saved_instancesr$  r=   r   s         rC   r	  z&BaseModelFormSet.save_existing_objects  s    !!!!I,,&& 	2D--C
 vv~&$$++C0$$S$8!!#$$++S$2C2C,DE&&t'9'9$F'9'ST$$++D1	2  rE   c                 <   g | _         | j                  D ]{  }|j                         s| j                  r| j	                  |      r2| j                   j                  | j                  ||             |ra| j                  j                  |       } | j                   S r;  )new_objectsextra_formsr?  
can_delete_should_delete_formr;   r   r  )ry   r   r=   s      rC   r
  z!BaseModelFormSet.save_new_objects  s    $$ 		.D##% 4#;#;D#A##DMM$vM$FG  ''-		. rE   c                   	
 ddl m	m}m} | j                  j
                  j                  x| _        }	
fd
 
|      s|j                  |j                  vr|j                  r9|j                  j                  j                  rdn|j                  j                  }n#	 || j                         |   j                  }nd}t!        |||f      r/|j"                  j                  j$                  j                         }n$| j                  j$                  j                         }|j'                  |j                  j                  j(                        }|j
                  j*                  r?|j
                  j*                  j-                  | j                  j                  t.              }nt.        }t1        ||d|      |j                  | j                  j                  <   t2        | i  ||       y# t        $ r d}Y :w xY w)z0Add a hidden field for the object's primary key.r   )r/   
ForeignKeyOneToOneFieldc                    | j                    xsr | j                  xs t        |       xsV | j                  xrH | j                  j                  xr0  | j                  j
                  j                  j                        S r|   )r-   auto_createdr.   r   parent_linkrW   r*   rR   )rR   r/   pk_is_not_editables    rC   rM  z7BaseModelFormSet.add_fields.<locals>.pk_is_not_editable  sn    [[ OO@z"i'@ OO K33K*2??+@+@+F+F+I+IJrE   NF)r   r   r3   )rT   r/   rH  rI  rW   r*   rR   	_pk_fieldr0   r,   r   r>   r   r   r   r   r.   r   r   usingdbrk   r8   r   r   r   
add_fields)ry   r=   indexrH  rI  rR   pk_valuer   r3   r/   rM  r   s            @@rC   rQ  zBaseModelFormSet.add_fields  s   II"jj..111		 b!RWWDKK%?}} $(==#7#7#>#>4DMMDTDT$(#'#4#4#6u#=#@#@#' "z=9:__**;;HHJZZ00==?$--..112Bzz!!++//0C0C[Q$/?HuV0DKK++, 	4' " $#H$s   ""G0 0G?>G?)NNr   NNr   )rh   r}   r~   __doc__rW   r  r   r  r{   r   r   r   r   r   r   r  r  r   r   r   r   r  r!  r  r	  r
  rQ  r   r   s   @rC   r!   r!     s     EI EM 
 
.,)
 D&((
V$ D[*z

E4 0( 0(rE   r!   r1  Fc                     t        |dd      }t        |d|      t        |d|	      t        d      t        | |||	||
|||||      }t        ||||||||||||      }| |_        ||_        |S )z8Return a FormSet class for the given Django model class.r   Nr,   r?   z]Calling modelformset_factory without defining 'fields' or 'exclude' explicitly is prohibited.)
r=   r,   r?   rl   rk   rm   rn   ro   re   rp   )
extramin_nummax_num	can_orderrE  validate_minvalidate_maxabsolute_maxcan_delete_extrar   )rJ   r   r%   r   rW   r  )rW   r=   rl   formsetrV  rE  rY  rX  r,   r?   rk   r[  rm   rn   ro   re   rW  rZ  rp   r\  r]  r   r  metaFormSets                            rC   r"   r"     s    4 4&Dh'/D)W-5"2
 	

 -)%#D !!!)G GM!GNrE   c                   t     e Zd ZdZ	 	 	 	 	 	 d	 fd	Z fdZ fdZed        Zd
 fd	Z	 fdZ
 fdZ xZS )r#   z0A formset for child objects related to a parent.c                    |*| j                   j                  j                         | _        n|| _        || _        || j                  j
                  }| j                  j                  3 |j                  di | j                   j                  | j                  i}n|j                         }| j                   j                  h| _
        t        	| 0  ||f||d| | j                  j                  j                  r| j                   j                  | j                  j                  j                  vrt!        | j                  j                  j                  t"              rBt%        | j                  j                  j                        | j                  j                  _        | j                  j                  j                  j'                  | j                   j                         y y y )N)r   rP   rS   )fkr   rW   r>   save_as_newr   rR   rV   r0   noner  r   r{   r=   r*   r,   r.   r  r  r;   )
ry   r5   r6   r>   rd  r   rP   ru   r   r   s
            rC   r{   zBaseInlineFormSet.__init__;  sM     GG00668DM$DM&zz22H==' ADGGLL$--#@ABB"ggll^uKVbKFK 99??!!dggll$))//:P:P&P$))//00%8)-diioo.D.D)E		&IIOO""))$'',,7 'Q!rE   c                 :    | j                   ryt        | 	         S )Nr   )rd  r   r   r   s    rC   r   z$BaseInlineFormSet.initial_form_countZ  s    w)++rE   c                 L   t        |   |fi |}| j                  rt        |j                  dd       }|d|j                  _        d |j                  |j                  | j                  j                        <   d |j                  |j                  | j                  j                        <   |||j                  _        | j                  j                  }| j                  j                  j                  | j                  j                  j                  j                  j                  j                  k7  rAt        | j                  | j                  j                  j                        }t        |d|      }t!        |j                  | j                  j#                         |       |S )N_mutableTrR   )r   r   rd  rJ   r5   rh  r7   rN  r0   rc  r>   rR   r   r7  rW   r*   setattrget_attname)ry   r   ru   r=   mutablefk_valuer   s         rC   r   z!BaseInlineFormSet._construct_form_  s4   w&q3F3diiT:G"%)		" ?CDIIdoodnn&9&9:;7;DIIdoodggll34"%,		" ==##77**dgg.B.B.H.H.N.N.Q.Q.V.VVt}}dgg.B.B.M.MNHxx8Htww224h?rE   c                     | j                   j                  j                  | j                        j	                  dd      S )N)rW   + )rc  r   get_accessor_namerW   replace)clss    rC   get_default_prefixz$BaseInlineFormSet.get_default_prefixv  s2    vv""443994EMMcSUVVrE   c                     t        |j                  | j                  j                  | j                         t        |   ||      S r;  )ri  r>   rc  r0   r   r   )ry   r=   r   r   s      rC   r   zBaseInlineFormSet.save_newz  s6     	tww||T]];wV44rE   c                    t         |   ||       | j                  | j                  k(  r| j                  j                  }ddi}n[| j                  j                  }dt        |j                  j                  |      dt        | j                  j                              i}| j                  j                  j                  | j                  j                  j                  j                  j                  j                  k7  r#| j                  j                  j                  |d<   | j                  j                   j"                  r|j                  d      )| j                  j                  j%                  |d         }n | j                  j                  j                  }|j'                         rx|j(                  | j                  j                  j                  j                  j                  j                  k(  s|j*                  s!t-        | j                  |j(                  d        t/        | j                  fi ||j                  |<   y )Npk_fieldTrc   to_field)r   rQ  rN  rc  r0   rJ   r,   r8   r   verbose_namer   r7  rW   r*   rR   r>   r   r   	get_fieldr1   attnamer5   ri  r   )ry   r=   rR  r0   ru   rw  r   s         rC   rQ  zBaseInlineFormSet.add_fields  s   4'>>TWW$>>&&D $'F 77<<DKKOOD)7HTWW=Q=Q4RF 77**dgg.B.B.H.H.N.N.Q.Q.V.VV!%!5!5!@!@F: ==&&zz*%1==..88
9KL==..11##%   DGG$8$8$>$>$D$D$G$G$L$LLyyx'7'7>1$--J6JDrE   c                 ~    |D cg c]  }|| j                   j                  k7  s|  }}t        |   |      S c c}w r|   )rc  r0   r   r  )ry   r)  r2   r   s      rC   r  z*BaseInlineFormSet.get_unique_error_message  s;    +7Q%5DGGLL;PQQw/== Rs   ::)NNNFNNr   )rh   r}   r~   rT  r{   r   r   classmethodrs  r   rQ  r  r   r   s   @rC   r#   r#   8  sU    : 8>,
. W W5%KN> >rE   r#   c                    ddl m} |j                  }|rd|j                  D cg c]  }|j                  |k(  s| }}t        |      dk(  r|d   }| j                  j                         }	t        ||      r|j                  j                  j                  j                  r,|j                  j                  j                  j                  |	vs[|j                  j                  j                  j                  s|j                  j                  | k7  rf|j                  j                  |	vrNt        d|d| j                  j                  d      |s&t        d|j                  j                  d|d      S | j                  j                         }	|j                  D cg c]  }t        ||      r|j                  j                  | k(  sn|j                  j                  |	v sV|j                  j                  j                  j                  r.|j                  j                  j                  j                  |	v r| }}t        |      dk(  r|d   }|S |s=|ry	t        d|j                  j                  d
| j                  j                  d      t        d|j                  j                  d| j                  j                  d      c c}w c c}w )aD  
    Find and return the ForeignKey from model to parent if there is one
    (return None if can_fail is True and no such field exists). If fk_name is
    provided, assume it is the name of the ForeignKey field. Unless can_fail is
    True, raise an exception if there isn't a ForeignKey from model to
    parent_model.
    r   )rH  r1  z	fk_name 'z' is not a ForeignKey to 'z'.ra   z' has no field named 'Nz' has no ForeignKey to 'z#' has more than one ForeignKey to 'z*'. You must specify a 'fk_name' attribute.)rT   rH  r*   r,   r0   r   get_parent_listr.   r   rW   proxyproxy_for_modelr   rc   )
parent_modelrW   fk_namecan_failrH  r@   rB   fks_to_parentrc  parent_lists
             rC   _get_foreign_keyr    sk    ,;;D$(KKEq166W3DEE}"q!B&,,<<>Kr:. OO))//55--33CC;V --3399--=--[@ ! 2 2 8 8:  38;;3D3DgN P IG #((88: [[
!Z($$4>>'';6NN((..44,,22BBkQ 
 
 }"q!B( I'  KK%% &&,,   KK%% &&,,	 o F:
s   KK"BKr6  c                     t        | ||      }|j                  rd}
i d|d|d|d|d|	d|d	|d
|d|d|
d|d|d|d|d|d|d||||||d}t        |fi |}||_        |S )z
    Return an ``InlineFormSet`` for the given kwargs.

    ``fk_name`` must be provided if ``model`` has more than one ``ForeignKey``
    to ``parent_model``.
    )r  r1  r=   rl   r^  rV  rE  rY  r,   r?   rW  rX  rk   rZ  r[  rm   rn   ro   re   )rp   r\  r]  r   r  )r  uniquer"   rc  )r  rW   r=   r^  r  r,   r?   rV  rY  rE  rX  rl   rk   r[  rm   rn   ro   re   rW  rZ  rp   r\  r]  r   r  rc  ru   r`  s                               rC   r$   r$     s#   B 
,w	?B	yy0 	7 		
 	j 	Y 	& 	7 	7 	7 	7 	 	 	, 	&  	j!" 	.#$ '$,-F0 #53F3GGJNrE   c                   N     e Zd ZdZeZd ed      iZddd fd
Zd Z	d	 Z
 xZS )
r   zw
    A basic integer field that deals with validating the given value to a
    given parent instance in an inline.
    invalid_choicez3The inline value did not match the parent instance.FN)rv  rw  c                   || _         || _        || _        | j                   I| j                  r$t        | j                   | j                        |d<   n| j                   j                  |d<   d|d<   t        |   |i | y )Nr   Fr   )parent_instancerv  rw  rJ   rR   r   r{   )ry   r  rv  rw  argsru   r   s         rC   r{   zInlineForeignKeyField.__init__L  sy    .  +}}$+D,@,@$--$Py!$($8$8$;$;y!"z$)&)rE   c                 P   || j                   v r| j                  ry | j                  S | j                  r!t	        | j                  | j                        }n| j                  j
                  }t        |      t        |      k7  rt        | j                  d   d      | j                  S )Nr  r   )	r9   rv  r  rw  rJ   rR   r   r   re   )ry   r   origs      rC   r   zInlineForeignKeyField.cleanX  s    D%%%}}'''==4//?D''**Du:T"!##$45<L  ###rE   c                      yNFrS   )ry   r   r5   s      rC   r?  z!InlineForeignKeyField.has_changedi  s    rE   )rh   r}   r~   rT  r   r3   r3  default_error_messagesr{   r   r?  r   r   s   @rC   r   r   A  s8    
 F!QR 9> 
*$"rE   r   c                   $    e Zd Zd Zd Zd Zd Zy)ModelChoiceIteratorValuec                      || _         || _        y r|   )r   r>   )ry   r   r>   s      rC   r{   z!ModelChoiceIteratorValue.__init__n  s    
 rE   c                 ,    t        | j                        S r|   )r   r   r   s    rC   __str__z ModelChoiceIteratorValue.__str__r  s    4::rE   c                 ,    t        | j                        S r|   )hashr   r   s    rC   __hash__z!ModelChoiceIteratorValue.__hash__u  s    DJJrE   c                 X    t        |t              r|j                  }| j                  |k(  S r|   )r.   r  r   )ry   others     rC   __eq__zModelChoiceIteratorValue.__eq__x  s%    e56KKEzzU""rE   N)rh   r}   r~   r{   r  r  r  rS   rE   rC   r  r  m  s    ! #rE   r  c                   *    e Zd Zd Zd Zd Zd Zd Zy)ModelChoiceIteratorc                 4    || _         |j                  | _        y r|   )r2   rP   r   s     rC   r{   zModelChoiceIterator.__init__  s    
rE   c              #      K   | j                   j                  d| j                   j                  f | j                  }|j                  s|j	                         }|D ]  }| j                  |        y w)Nro  )r2   empty_labelrP   _prefetch_related_lookupsiteratorchoice)ry   rP   r   s      rC   __iter__zModelChoiceIterator.__iter__  si     ::!!-tzz--..==11((*H 	#C++c""	#s   A4A6c                 p    | j                   j                         | j                  j                  dz   S dz   S )Nr1  r   )rP   countr2   r  r   s    rC   __len__zModelChoiceIterator.__len__  s4     }}""$TZZ-C-C-OWWUVWWrE   c                 j    | j                   j                  d uxs | j                  j                         S r|   )r2   r  rP   existsr   s    rC   __bool__zModelChoiceIterator.__bool__  s)    zz%%T1KT]]5I5I5KKrE   c                     t        | j                  j                  |      |      | j                  j                  |      fS r|   )r  r2   prepare_valuelabel_from_instancery   r   s     rC   r  zModelChoiceIterator.choice  s7    $TZZ%=%=c%BCHJJ**3/
 	
rE   N)rh   r}   r~   r{   r  r  r  r  rS   rE   rC   r  r  ~  s    '#XL
rE   r  c            
            e Zd ZdZd ed      iZeZdddddddddd		d
Zd Z	 fdZ
d Zd Z eee      Zd Zd Z eeej$                  j&                        Z fdZd Zd Zd Z xZS )r   z1A ChoiceField whose choices are a model QuerySet.r  zGSelect a valid choice. That choice is not one of the available choices.z	---------TNro  F)	r  r   r3   rc   r   rd   to_field_namerZ   r   c       	   	          t        j                  | f|||||d| |r|t        | j                  t              r
|
sd | _        n|| _        || _        |	| _        || _        y )N)r   r3   rc   r   rd   )	r
   r{   r.   r3   r   r  rP   rZ   r  )ry   rP   r  r   r3   rc   r   rd   r  rZ   r   ru   s               rC   r{   zModelChoiceField.__init__  sr    " 		
	
 	
 ,t{{K0#D*D  0*rE   c                 d    t        | j                        r| j                         S | j                  S )z
        Return ``limit_choices_to`` for this form field.

        If it is a callable, invoke it and return the result.
        )ri   rZ   r   s    rC   rQ   z%ModelChoiceField.get_limit_choices_to  s-     D))*((**$$$rE   c                     t         t        |   |      }| j                  | j                  j	                         |_        |S r|   )r   r	   __deepcopy__rP   all)ry   memoresultr   s      rC   r  zModelChoiceField.__deepcopy__  s7    {D6t<==$"mm//1FOrE   c                     | j                   S r|   )r   r   s    rC   _get_querysetzModelChoiceField._get_queryset  s    ~~rE   c                 l    |d n|j                         | _        | j                  | j                  _        y r|   )r  r   choicesr3   )ry   rP   s     rC   _set_querysetzModelChoiceField._set_queryset  s&    !)!1x||~"llrE   c                     t        |      S )z
        Convert objects into strings and generate the labels for the choices
        presented by this object. Subclasses can override this method to
        customize the display of the choices.
        )r   r  s     rC   r  z$ModelChoiceField.label_from_instance  s     3xrE   c                 T    t        | d      r| j                  S | j                  |       S )N_choices)rU   r  r  r   s    rC   _get_choiceszModelChoiceField._get_choices  s)     4$==  }}T""rE   c                     t        |d      r3| j                  r|j                  | j                        S |j                  S t        |   |      S Nr*   )rU   r  serializable_valuerR   r   r  )ry   r   r   s     rC   r  zModelChoiceField.prepare_value  sF    5'"!!//0B0BCCxxw$U++rE   c                 x   || j                   v ry 	 | j                  xs d}t        || j                  j                        rt        ||      } | j                  j                  di ||i}|S # t        t        | j                  j                  j                  f$ r t        | j                  d   dd|i      w xY w)NrR   r  r   r   paramsrS   )r9   r  r.   rP   rW   rJ   r8   r   rj   DoesNotExistr   re   )ry   r   keys      rC   r   zModelChoiceField.to_python
  s    D%%%
	$$,C%!4!45s+%DMM%%5e5E  It}}':':'G'GH 	!##$45%' 	s   AA- -AB9c                 .    t        j                  | |      S r|   )r
   validatery   r   s     rC   r  zModelChoiceField.validate  s    ~~dE**rE   c                     | j                   ry||nd}||nd}t        | j                  |            t        |      k7  S )NFro  )disabledr   r  )ry   r   r5   initial_value
data_values        rC   r?  zModelChoiceField.has_changed  sC    ==#*#6B!-T2
4%%m45ZHHrE   )rh   r}   r~   rT  r3  r  r  r  r{   rQ   r  r  r  propertyrP   r  r  r	   r  fsetr  r   r  r?  r   r   s   @rC   r   r     s    ;
 	!U

 #H  "+H%+ }5H# |[%8%8%=%=>G, +IrE   r   c                   |     e Zd ZdZeZeZ ed       ed       ed      dZ	 fdZ
d Zd Zd	 Z fd
Zd Z xZS )r   z9A MultipleChoiceField whose choices are a model QuerySet.zEnter a list of values.zESelect a valid choice. %(value)s is not one of the available choices.u"   “%(pk)s” is not a valid value.)invalid_listr  invalid_pk_valuec                 *    t        |   |fdd i| y )Nr  )r   r{   )ry   rP   ru   r   s      rC   r{   z!ModelMultipleChoiceField.__init__2  s    >t>v>rE   c                 >    |sg S t        | j                  |            S r|   )r  _check_valuesr  s     rC   r   z"ModelMultipleChoiceField.to_python5  s     ID&&u-..rE   c                 l   | j                  |      }| j                  r|st        | j                  d   d      | j                  s|s| j                  j                         S t        |t        t        f      st        | j                  d   d      | j                  |      }| j                  |       |S )Nr   r  r  )r  r   r   re   rP   re  r.   r  r  r  run_validators)ry   r   r   s      rC   r   zModelMultipleChoiceField.clean:  s    ""5)==!$"5"5j"A
SSu==%%''%$/!##N3#  & 	E"	rE   c           	      *   | j                   xs d}	 t        |      }|D ]!  }	  | j
                  j                  d	i ||i #  | j
                  j                  d	i d|z  |i}|D ch c]  }t        t        ||             }}|D ],  }t        |      |vst        | j                  d   dd|i       |S # t        $ r t        | j                  d   d      w xY w# t        t        f$ r t        | j                  d   dd|i      w xY wc c}w )
z
        Given a list of possible PK values, return a QuerySet of the
        corresponding objects. Raise a ValidationError if a given value is
        invalid (not a valid PK, not in the queryset, etc.)
        rR   r  r  r  r  z%s__inr  r   rS   )
r  	frozensetrj   r   re   rP   rV   r   r   rJ   )ry   r   r  rR   r   r   pksvals           rC   r  z&ModelMultipleChoiceField._check_valuesK  sL      (D	e$E  	B$$$1Ry1	 "T]]!!<X^U$;<-/0s71c?#00 	C3xs"%''(89)#S> 	 	1  	!##N3# 	 	* %''(:;+ ":  1s   B9 C (D9$C -Dc                     t        |d      r<t        |t              s,t        |d      s t        |   }|D cg c]
  } ||       c}S t        |   |      S c c}w )Nr  r*   )rU   r.   r   r   r  )ry   r   r  r   r   s       rC   r  z&ModelMultipleChoiceField.prepare_valuep  sV    E:&uc*E7+!G1M.34M!$44w$U++ 5s   Ac                     | j                   ry|g }|g }t        |      t        |      k7  ry| j                  |      D ch c]  }t        |       }}|D ch c]  }t        |       }}||k7  S c c}w c c}w )NFT)r  r   r  r   )ry   r   r5   r   initial_setdata_sets         rC   r?  z$ModelMultipleChoiceField.has_changedz  s    ==?G<Dw<3t9$/3/A/A'/JKes5zKK,015CJ11;&& L1s   A2A7)rh   r}   r~   rT  r   r3   r   hidden_widgetr3  r  r{   r   r   r  r  r?  r   r   s   @rC   r   r   %  sV    CF'M34S
 BC?/
"#J,'rE   r   c                     t        | d      xr2 | j                  j                  d uxs | j                  j                  d uS r  )rU   r*   r,   r?   )rf   s    rC   modelform_defines_fieldsr    s@    :w' t+Sz/?/?/G/Gt/SrE   )NN)	NNNNNNNNNr  )9rT  	itertoolsr   django.core.exceptionsr   r   r   r   django.db.models.utilsr   django.forms.fieldsr	   r
   django.forms.formsr   r   django.forms.formsetsr   r   django.forms.utilsr   django.forms.widgetsr   r   r   r   django.utils.choicesr   django.utils.textr   r   django.utils.translationr   r   r3  r&   r    rD   r   r\   r   rw   r   r   r   r%   r!   r"   r#   r  r$   r   r  r  r   r   r  rS   rE   rC   <module>r     s     . 2 C > (  4 5 , 6 
*`2& s  slO OF3 FRYHj Yx	); 	 
U=v]({J ](D 

/ANr>( r>jLd 

3?J)E )X# #"
, 
>EI{ EIP`'/ `'FrE   