
    Hwg                     z   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mZ ddlmZ ddlmZ  dZ!dZ"d5dZ#d5dZ$d Z%	 	 	 	 	 	 	 	 	 d6d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,      Z.d7d'Z/e*e.dddd(d#ddddd#dddddd#ddddd#fd)Z0 G d* d+e      Z1 G d, d-      Z2 G d. d/      Z3 G d0 d1e      Z4 G d2 d3e4      Z5d4 Z6y)8z[
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)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	            J/var/www/horilla/myenv/lib/python3.12/site-packages/django/forms/models.pyconstruct_instancerC   .   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getattrr/   value_from_object)r=   r+   r>   r?   r4   rA   s         rB   r   r   ^   s     >>DD4'')<)<d>O>OP 5q*e,!&&"6qvv(**84QVV5 KrD   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)rQ   N )django.db.modelsrL   rM   rN   hasattrrP   r-   rO   filtermodel_base_manager)	formfieldrL   rM   rN   limit_choices_tocomplex_filters         rB   #apply_limit_choices_to_to_formfieldr[   w   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%rD   Tapply_limit_choices_toc
                   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zi }|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 )a4  
    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.
    r   )r
   r,   F'z' cannot be specified for z) model form as it is a non-editable fieldr2   Tlocalizelabel	help_texterror_messages
form_classz1formfield_callback must be a function or callablerR   )r)   rS   r
   rG   r-   sortedr   rF   rH   rI   r/   r   __name__r   rX   callable	TypeErrorr[   r:   r7   )rV   r+   r>   widgetsformfield_callbacklocalized_fieldslabels
help_textsrc   field_classesr]   
field_dictignoredr?   
ModelFieldrA   sortable_private_fieldskwargsrX   s                      rB   r   r      s{   X JG;;D4 &&*Q
*C  d""$;T=N=NO /# q*e,"FFf$_g(= ,-FFENND  !&&"6qvv(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"_/#`  
q/Qg5E z~~a  

 

 sh
s   I
I
##I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 )NrV   r+   r>   ri   rk   rl   rm   rc   rn   rj   )rI   rV   r+   r>   ri   rk   rl   rm   rc   rn   rj   )selfoptionss     rB   __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rD   N)rf   
__module____qualname__ry   rR   rD   rB   ru   ru      s    
OrD   ru   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                  rO|j                  |j                  t        d|z        |j                  t        k(  rd |_        t        |j                  |j                  |j                  |j                   |j"                  |j$                  |j&                  |j(                  |j*                  |j,                  d      }	|	j/                         D 
ch c]
  \  }
}|r	|
 }}
}|j1                  |j2                        }|r7d}|d	j5                  |      |j                  j                  fz  }t7        |      |	j9                  |j2                         n|j2                  }	|	|_        |S c c}}
w )
NMeta)r+   r>   rk   zP%(model)s.Meta.%(opt)s cannot be a string. Did you mean to type: ('%(value)s',)?)rV   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   ru   rI   r)   r-   strr   rf   rh   rV   r+   r>   r   r   ri   rj   rk   rl   rm   rc   rn   items
differencedeclared_fieldsjoinr   updatebase_fields)mcsr/   basesattrs	new_classr?   r   r   msgr+   kvnone_model_fieldsmissing_fieldsmessage	__class__s                  rB   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   "
G9-G9)rf   r{   r|   r   __classcell__r   s   @rB   r~   r~     s    E ErD   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)   rV   
ValueErrorr=   r   r+   r>   r   _validate_uniquer   ry   valuesr[   )rw   r4   r5   auto_idprefixinitialerror_classlabel_suffixempty_permittedr=   r   r   r?   object_datarX   r   s                  rB   ry   zBaseModelForm.__init__N  s     zz::FGG JJLDMK$DM'$++t||LKw' !&#9 	 	
 ++- 	;I/	:	;rD   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+   r/   addr>   _errorsr*   r7   blankrequiredr8   )rw   r>   rA   r1   
form_fieldfield_values         rB   _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 rD   c                 (    d| _         | j                  S )NT)r   r*   rw   s    rB   cleanzBaseModelForm.clean  s     $   rD   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)   rT   r   r   r   rc   r+   r-   r   coder   	add_error)rw   errorsr?   r   r1   messagesrc   r   s           rB   _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$rD   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   rC   r=   r>   r   r   
full_cleanr   r   )rw   r?   r>   r/   r1   es         rB   _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   )rw   r>   r   s      rB   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   rH   rG   rT   r/   r;   )rw   r*   r>   r+   r?   rA   s         rB   	_save_m2mzBaseModelForm._save_m2m  s     ((**$$""}}"" t(($*=*=> 	FA1./!&&.166W,vv%  QVV0DE	FrD   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)rw   commits     rB   r   zBaseModelForm.save  s     ;; MM''33!%!5!5!<!<I)K  MM NN
 }} !NNDM}}rD   T)rf   r{   r|   r   ry   r   r   r   r   r   r   r   alters_datar   r   s   @rB   r   r   M  s`     #,;\+Z!%B#@	#F*0 DrD   r   c                       e Zd Zy)r   N)rf   r{   r|   rR   rD   rB   r   r   )  s    rD   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.
    rV   Nr+   r>   ri   rk   rl   rm   rc   rn   r   rR   FormzZCalling modelform_factory without defining 'fields' or 'exclude' explicitly is prohibited.)rT   r   typestaticmethodrj   rf   rI   r   )rV   r<   r+   r>   rj   ri   rk   rl   rm   rc   rn   r   r   r   
class_nameform_class_attrss                   rB   r$   r$   -  s)   ` eE h"i"i#$4 ! h(l!"0 !.o $D&1TYYLrEu%D"./A"B&(J ~tXt$,y$1O1W"2
 	
 4:j4'+;<<rD   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)r4   r5   r   r   rR   )rO   initial_extrar   ry   )	rw   r4   r5   r   r   rO   r   rs   r   s	           rB   ry   zBaseModelFormSet.__init__  sA     !$ 	
" 	
 	
rD   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rw   r   s    rB   r   z#BaseModelFormSet.initial_form_count  s,    }}t((*++w)++rD   c                     t        | d      s.| j                         D ci c]  }|j                  | c}| _        | j                  j	                  |      S c c}w )N_object_dict)rT   r   rQ   r   r7   )rw   rQ   os      rB   _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rw   r1   s     rB   _get_to_pythonzBaseModelFormSet._get_to_python  s<    
   ,&&88:E   ,rD   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   r6   rV   r)   rQ   r/   r4   r   r   r   KeyErrorr   r   
IndexErrorr   _construct_formr+   r   )	rw   irs   pk_requiredpk_keyrQ   r   r<   r   s	           rB   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)rT   rO   rV   _default_managerr   orderedorder_byr)   rQ   r/   r   )rw   qss     rB   r   zBaseModelFormSet.get_queryset  sw    t[)}}(]]ZZ00==?
 ::[[!1!1!4!4!9!9:
  DN~~rD   Tc                 &    |j                  |      S )z8Save and return a new model instance for the given form.r   r   )rw   r<   r   s      rB   save_newzBaseModelFormSet.save_new      yyy''rD   c                 &    |j                  |      S )z>Save and return an existing model instance for the given form.r   r   )rw   r<   r=   r   s       rB   save_existingzBaseModelFormSet.save_existing  r   rD   c                 *    |r|j                          yy)z#Deletes an existing model instance.N)delete)rw   objr   s      rB   delete_existingz BaseModelFormSet.delete_existing  s    JJL rD   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 rz   )saved_formsr   )r<   rw   s    rB   r   z'BaseModelFormSet.save.<locals>.save_m2m  s!     ,, $DMMO$rD   )r  r   	edit_onlysave_existing_objectssave_new_objects)rw   r   r   s   `  rB   r   zBaseModelFormSet.save   sW    
 !D$ %DM>>--f55--f58M8Mf8UUUrD   c                 $    | j                          y rz   )r   r   s    rB   r   zBaseModelFormSet.clean  s    rD   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rz   )r*   unique_fields).0r1   r<   rw   s     rB   	<genexpr>z3BaseModelFormSet.validate_unique.<locals>.<genexpr>1  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)rT   r  r-   listtuple)r  ds     rB   r  z3BaseModelFormSet.validate_unique.<locals>.<genexpr>7  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dayrI   get_date_error_messager   )rw   all_unique_checksall_date_checksforms_to_deleter<   valid_formsr>   unique_checksdate_checksr   uclassunique_check	seen_datarow_datar1   
date_checklookup
unique_forr  	date_datar4   s   `   `                rB   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.r1   r   zFPlease correct the duplicate data for %(field)s, which must be unique.and)r   r   r   _)rw   r'  s     rB   r  z)BaseModelFormSet.get_unique_error_messaget  s_    |!MNaR   X |QuX> rD   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.      r/  )
field_name
date_fieldr+  )r   r   )rw   r*  s     rB   r  z'BaseModelFormSet.get_date_error_message  s;    I
 %Q-$Q-*Q-(

 	
rD   c                     t        d      S )Nz*Please correct the duplicate values below.r   r   s    rB   r  zBaseModelFormSet.get_form_error  s    CDDrD   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=   rQ   r:   r  has_changedchanged_datar   r  )rw   r   saved_instancesr"  r<   r  s         rB   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  rD   c                 <   g | _         | j                  D ]{  }|j                         s| j                  r| j	                  |      r2| j                   j                  | j                  ||             |ra| j                  j                  |       } | j                   S r9  )new_objectsextra_formsr=  
can_delete_should_delete_formr:   r   r  )rw   r   r<   s      rB   r  z!BaseModelFormSet.save_new_objects  s    $$ 		.D##% 4#;#;D#A##DMM$vM$FG  ''-		. rD   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 rz   )r,   auto_createdr-   r   parent_linkrV   r)   rQ   )rQ   r.   pk_is_not_editables    rB   rK  z7BaseModelFormSet.add_fields.<locals>.pk_is_not_editable  sn    [[ OO@z"i'@ OO K33K*2??+@+@+F+F+I+IJrD   NF)r   r   r2   )rS   r.   rF  rG  rV   r)   rQ   	_pk_fieldr/   r+   r   r=   r   r   r   r   r-   r   r   usingdbri   r7   r   r   r   
add_fields)rw   r<   indexrF  rG  rQ   pk_valuer   r2   r.   rK  r   s            @@rB   rO  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   )rf   r{   r|   __doc__rV   r  r   r  ry   r   r   r   r   r   r   r   r  r   r   r   r   r  r  r  r  r  rO  r   r   s   @rB   r    r      s     EI EM 
 
.,)
 D&((
V$ D[*z

E4 0( 0(rD   r    r/  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>   rj   ri   rk   rl   rm   rc   rn   )
extramin_nummax_num	can_orderrC  validate_minvalidate_maxabsolute_maxcan_delete_extrar   )rI   r   r$   r   rV   r  )rV   r<   rj   formsetrT  rC  rW  rV  r+   r>   ri   rY  rk   rl   rm   rc   rU  rX  rn   rZ  r[  r   r  metaFormSets                            rB   r!   r!     s    4 4&Dh'/D)W-5"2
 	

 -)%#D !!!)G GM!GNrD   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   rO   rR   )fkr   rV   r=   save_as_newr   rQ   rU   r/   noner  r   ry   r<   r)   r+   r-   r  r  r:   )
rw   r4   r5   r=   rb  r   rO   rs   r   r   s
            rB   ry   zBaseInlineFormSet.__init__2  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!rD   c                 :    | j                   ryt        | 	         S )Nr   )rb  r   r   r   s    rB   r   z$BaseInlineFormSet.initial_form_countQ  s    w)++rD   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_mutableTrQ   )r   r   rb  rI   r4   rf  r6   rL  r/   ra  r=   rQ   r   r5  rV   r)   setattrget_attname)rw   r   rs   r<   mutablefk_valuer   s         rB   r   z!BaseInlineFormSet._construct_formV  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?rD   c                     | j                   j                  j                  | j                        j	                  dd      S )N)rV   + )ra  r   get_accessor_namerV   replace)clss    rB   get_default_prefixz$BaseInlineFormSet.get_default_prefixm  s2    vv""443994EMMcSUVVrD   c                     t        |j                  | j                  j                  | j                         t        |   ||      S r9  )rg  r=   ra  r/   r   r   )rw   r<   r   r   s      rB   r   zBaseInlineFormSet.save_newq  s6     	tww||T]];wV44rD   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'                         r!t)        | j                  |j*                  d        t-        | j                  fi ||j                  |<   y )Npk_fieldTra   to_field)r   rO  rL  ra  r/   rI   r+   r7   r   verbose_namer   r5  rV   r)   rQ   r=   r   r   	get_fieldr0   rg  attnamer   )rw   r<   rP  r/   rs   ru  r   s         rB   rO  zBaseInlineFormSet.add_fieldsx  sj   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##%x'7'7>1$--J6JDrD   c                 ~    |D cg c]  }|| j                   j                  k7  s|  }}t        |   |      S c c}w rz   )ra  r/   r   r  )rw   r'  r1   r   s      rB   r  z*BaseInlineFormSet.get_unique_error_message  s;    +7Q%5DGGLL;PQQw/== Rs   ::)NNNFNNr   )rf   r{   r|   rR  ry   r   r   classmethodrq  r   rO  r  r   r   s   @rB   r"   r"   /  sU    : 8>,
. W W5KB> >rD   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   )rF  r/  z	fk_name 'z' is not a ForeignKey to 'z'.r_   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.)rS   rF  r)   r+   r/   r   get_parent_listr-   r   rV   proxyproxy_for_modelr   ra   )
parent_modelrV   fk_namecan_failrF  r?   rA   fks_to_parentra  parent_lists
             rB   _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r4  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  r/  r<   rj   r\  rT  rC  rW  r+   r>   rU  rV  ri   rX  rY  rk   rl   rm   rc   )rn   rZ  r[  r   r  )r  uniquer!   ra  )r  rV   r<   r\  r  r+   r>   rT  rW  rC  rV  rj   ri   rY  rk   rl   rm   rc   rU  rX  rn   rZ  r[  r   r  ra  rs   r^  s                               rB   r#   r#     s#   B 
,w	?B	yy0 	7 		
 	j 	Y 	& 	7 	7 	7 	7 	 	 	, 	&  	j!" 	.#$ '$,-F0 #53F3GGJNrD   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)rt  ru  c                   || _         || _        || _        | j                   I| j                  r$t        | j                   | j                        |d<   n| j                   j                  |d<   d|d<   t        |   |i | y )Nr   Fr   )parent_instancert  ru  rI   rQ   r   ry   )rw   r  rt  ru  argsrs   r   s         rB   ry   zInlineForeignKeyField.__init__=  sy    .  +}}$+D,@,@$--$Py!$($8$8$;$;y!"z$)&)rD   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   )	r8   rt  r  ru  rI   rQ   r   r   rc   )rw   r   origs      rB   r   zInlineForeignKeyField.cleanI  s    D%%%}}'''==4//?D''**Du:T"!##$45<L  ###rD   c                      yNFrR   )rw   r   r4   s      rB   r=  z!InlineForeignKeyField.has_changedZ  s    rD   )rf   r{   r|   rR  r   r2   r1  default_error_messagesry   r   r=  r   r   s   @rB   r   r   2  s8    
 F!QR 9> 
*$"rD   r   c                   $    e Zd Zd Zd Zd Zd Zy)ModelChoiceIteratorValuec                      || _         || _        y rz   )r   r=   )rw   r   r=   s      rB   ry   z!ModelChoiceIteratorValue.__init___  s    
 rD   c                 ,    t        | j                        S rz   )r   r   r   s    rB   __str__z ModelChoiceIteratorValue.__str__c  s    4::rD   c                 ,    t        | j                        S rz   )hashr   r   s    rB   __hash__z!ModelChoiceIteratorValue.__hash__f  s    DJJrD   c                 X    t        |t              r|j                  }| j                  |k(  S rz   )r-   r  r   )rw   others     rB   __eq__zModelChoiceIteratorValue.__eq__i  s%    e56KKEzzU""rD   N)rf   r{   r|   ry   r  r  r  rR   rD   rB   r  r  ^  s    ! #rD   r  c                   *    e Zd Zd Zd Zd Zd Zd Zy)ModelChoiceIteratorc                 4    || _         |j                  | _        y rz   )r1   rO   r   s     rB   ry   zModelChoiceIterator.__init__p  s    
rD   c              #      K   | j                   j                  d| j                   j                  f | j                  }|j                  s|j	                         }|D ]  }| j                  |        y w)Nrm  )r1   empty_labelrO   _prefetch_related_lookupsiteratorchoice)rw   rO   r  s      rB   __iter__zModelChoiceIterator.__iter__t  si     ::!!-tzz--..==11((*H 	#C++c""	#s   A4A6c                 p    | j                   j                         | j                  j                  dz   S dz   S )Nr/  r   )rO   countr1   r  r   s    rB   __len__zModelChoiceIterator.__len__~  s4     }}""$TZZ-C-C-OWWUVWWrD   c                 j    | j                   j                  d uxs | j                  j                         S rz   )r1   r  rO   existsr   s    rB   __bool__zModelChoiceIterator.__bool__  s)    zz%%T1KT]]5I5I5KKrD   c                     t        | j                  j                  |      |      | j                  j                  |      fS rz   )r  r1   prepare_valuelabel_from_instancerw   r  s     rB   r  zModelChoiceIterator.choice  s7    $TZZ%=%=c%BCHJJ**3/
 	
rD   N)rf   r{   r|   ry   r  r  r  r  rR   rD   rB   r  r  o  s    '#XL
rD   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$                        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	---------TNrm  F)	r  r   r2   ra   r   rb   to_field_namerY   r   c       	   	          t        j                  | f|||||d| |r|t        | j                  t              r
|
sd | _        n|| _        || _        |	| _        || _        y )N)r   r2   ra   r   rb   )	r
   ry   r-   r2   r   r  rO   rY   r  )rw   rO   r  r   r2   ra   r   rb   r  rY   r   rs   s               rB   ry   zModelChoiceField.__init__  sr    " 		
	
 	
 ,t{{K0#D*D  0*rD   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.
        )rg   rY   r   s    rB   rP   z%ModelChoiceField.get_limit_choices_to  s-     D))*((**$$$rD   c                     t         t        |   |      }| j                  | j                  j	                         |_        |S rz   )r   r	   __deepcopy__rO   all)rw   memoresultr   s      rB   r  zModelChoiceField.__deepcopy__  s7    {D6t<==$"mm//1FOrD   c                     | j                   S rz   )r   r   s    rB   _get_querysetzModelChoiceField._get_queryset  s    ~~rD   c                 l    |d n|j                         | _        | j                  | j                  _        y rz   )r  r   choicesr2   )rw   rO   s     rB   _set_querysetzModelChoiceField._set_queryset  s&    !)!1x||~"llrD   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     rB   r  z$ModelChoiceField.label_from_instance  s     3xrD   c                 T    t        | d      r| j                  S | j                  |       S )N_choices)rT   r  r  r   s    rB   _get_choiceszModelChoiceField._get_choices  s)     4$==  }}T""rD   c                     t        |d      r3| j                  r|j                  | j                        S |j                  S t        |   |      S Nr)   )rT   r  serializable_valuerQ   r   r  )rw   r   r   s     rB   r  zModelChoiceField.prepare_value  sF    5'"!!//0B0BCCxxw$U++rD   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)NrQ   r  r   r   paramsrR   )r8   r  r-   rO   rV   rI   r7   r   rh   DoesNotExistr   rc   )rw   r   keys      rB   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 rz   )r
   validaterw   r   s     rB   r  zModelChoiceField.validate  s    ~~dE**rD   c                     | j                   ry||nd}||nd}t        | j                  |            t        |      k7  S )NFrm  )disabledr   r  )rw   r   r4   initial_value
data_values        rB   r=  zModelChoiceField.has_changed  sC    ==#*#6B!-T2
4%%m45ZHHrD   )rf   r{   r|   rR  r1  r  r  r  ry   rP   r  r  r  propertyrO   r  r  r	   _set_choicesr  r  r   r  r=  r   r   s   @rB   r   r     s    ;
 	!U

 #H  "+H%+ }5H# |[%=%=>G, +IrD   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   ry   )rw   rO   rs   r   s      rB   ry   z!ModelMultipleChoiceField.__init__#  s    >t>v>rD   c                 >    |sg S t        | j                  |            S rz   )r  _check_valuesr  s     rB   r   z"ModelMultipleChoiceField.to_python&  s     ID&&u-..rD   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   rc   rO   rc  r-   r  r  r  run_validators)rw   r   r   s      rB   r   zModelMultipleChoiceField.clean+  s    ""5)==!$"5"5j"A
SSu==%%''%$/!##N3#  & 	E"	rD   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.)
        rQ   r  r  r  r  z%s__inr  r   rR   )
r  	frozensetrh   r   rc   rO   rU   r   r   rI   )rw   r   r  rQ   r   r   pksvals           rB   r  z&ModelMultipleChoiceField._check_values<  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)   )rT   r-   r   r   r  )rw   r   r  r   r   s       rB   r  z&ModelMultipleChoiceField.prepare_valuea  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   )rw   r   r4   r   initial_setdata_sets         rB   r=  z$ModelMultipleChoiceField.has_changedk  s    ==?G<Dw<3t9$/3/A/A'/JKes5zKK,015CJ11;&& L1s   A2A7)rf   r{   r|   rR  r   r2   r   hidden_widgetr1  r  ry   r   r   r  r  r=  r   r   s   @rB   r   r     sV    CF'M34S
 BC?/
"#J,'rD   r   c                     t        | d      xr2 | j                  j                  d uxs | j                  j                  d uS r  )rT   r)   r+   r>   )rd   s    rB   modelform_defines_fieldsr  y  s@    :w' t+Sz/?/?/G/Gt/SrD   )NN)	NNNNNNNNNr  )7rR  	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.textr   r   django.utils.translationr   r   r1  r%   r   rC   r   r[   r   ru   r~   r   r   r$   r    r!   r"   r  r#   r   r  r  r   r   r  rR   rD   rB   <module>r     s     . 2 C > (  6 , 6 
*`2& k  k\O OF3 FRYHj Yx	); 	 
U=v]({J ](D 

/ANl>( l>^Ld 

3?J)E )X# #"
 
>EI{ EIP`'/ `'FrD   