
v^                 @   s  d  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 m Z d d l m Z m Z d d l m Z d d l m Z m Z m Z d d	 l m Z m Z d d
 l m Z m Z d d d d d d d d d d d d f Z d Z d d d d  Z  d d d d  Z! d d   Z" d d d d d d d d d d d d  d 	Z# Gd! d"   d"  Z$ Gd# d$   d$ e  Z% Gd% d   d e  Z& Gd& d   d e& d' e% Z' e' d d d d d d d d d d( d 
 Z( Gd) d   d e  Z) e' d e) d* d+ d+ d d d d d+ d d d d d d+ d d, d  Z* Gd- d   d e)  Z+ d d+ d. d/  Z, e' e+ d d d d0 d+ d d d d d+ d d d d d d+ d d1 d  Z- Gd2 d3   d3 e
  Z. Gd4 d5   d5  Z/ Gd6 d   d e	  Z0 Gd7 d   d e0  Z1 d8 d9   Z2 d S):z[
Helper functions for creating Form classes from Django models
and database field objects.
    )chain)NON_FIELD_ERRORS
FieldErrorImproperlyConfiguredValidationError)ChoiceFieldField)BaseFormDeclarativeFieldsMetaclass)BaseFormSetformset_factory)	ErrorList)HiddenInputMultipleHiddenInput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       	      C   sm  d d l  m } | j } |  j } g  } x| j D]} | j s2 t | | j  s2 | j | k rf q2 | d k	 r | j | k r q2 | r | j | k r q2 | j	   r|  | j j
 j j |  j |  j |  j | j   r| j | j  |  | j j
 j k rq2 t | | j  r&| j |  q2 | j | | | j  q2 Wx% | D] } | j | | | j  qHW| 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   )modelsN)Z	django.dbr"   _metacleaned_datafieldseditable
isinstance	AutoFieldnamehas_defaultfieldwidgetZvalue_omitted_from_datadatafiles
add_prefixgetempty_valuesZ	FileFieldappendsave_form_data)	forminstancer%   excluder"   optsr$   Zfile_field_listf r9   5/tmp/pip-build-8lau8j11/django/django/forms/models.pyconstruct_instance   s,    		4%r;   c             C   s   |  j  } i  } x t | j | j | j  D]g } t | d d  sF q+ | d k	 rd | j | k rd q+ | r| | j | k r| q+ | j |   | | j <q+ W| 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&   FN)r#   r   concrete_fieldsprivate_fieldsmany_to_manygetattrr)   Zvalue_from_object)r5   r%   r6   r7   r-   r8   r9   r9   r:   r   H   s    	"c             C   sO   t  |  d  rK t  |  d  rK |  j   } | d k	 rK |  j j |  |  _ d S)z=Apply limit_choices_to to the formfield's queryset if needed.querysetget_limit_choices_toN)hasattrrA   r@   Zcomplex_filter)	formfieldlimit_choices_tor9   r9   r:   #apply_limit_choices_to_to_formfielda   s    rE   apply_limit_choices_toTc
               s  i   g   |  j  } d d l m     f d d   | j D } x0t t | j | | j   D]} t | d d  s | d k	 r` | j	 | k r`  d k s | j	  k r` t
 d | j	 |  j f   q` | d k	 r | j	 | k r q`  r| j	  k rq` i  } | r/| j	 | k r/| | j	 | d	 <| t k sP| rZ| j	 | k rZd
 | d <| r| j	 | k r| | j	 | d <| r| j	 | k r| | j	 | d <| r| j	 | k r| | j	 | d <|	 r| j	 |	 k r|	 | j	 | d <| d k r| j |   } n* t |  s+t d   n | | |  } | r`|
 rPt |  |  | j	 <q`  j | j	  q` W| r   f d d   | D   S)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   c                s%   g  |  ] } t  |    r |  q Sr9   )r'   ).0r8   )
ModelFieldr9   r:   
<listcomp>   s   	 z$fields_for_model.<locals>.<listcomp>r&   FNzH'%s' cannot be specified for %s model form as it is a non-editable fieldr,   TZlocalizelabel	help_texterror_messages
form_classz1formfield_callback must be a function or callablec                sA   i  |  ]7 }   s |   k r |  k r  j  |  |  q Sr9   )r0   )rG   r8   )r6   
field_dictignoredr9   r:   
<dictcomp>   s   	z$fields_for_model.<locals>.<dictcomp>)r#   Zdjango.db.models.fieldsr   r=   sortedr   r<   r>   r?   r)   r   __name__r   rC   callable	TypeErrorrE   r2   )modelr%   r6   widgetsformfield_callbacklocalized_fieldslabels
help_textsrL   field_classesrF   r7   Zsortable_private_fieldsr8   kwargsrC   r9   )rH   r6   rN   rO   r:   r   i   sX    "	%!


c               @   s   e  Z d  Z d d d  Z d S)ModelFormOptionsNc             C   s   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   |  _	 d  S)
NrU   r%   r6   rV   rX   rY   rZ   rL   r[   )
r?   rU   r%   r6   rV   rX   rY   rZ   rL   r[   )selfoptionsr9   r9   r:   __init__   s    zModelFormOptions.__init__)rR   
__module____qualname__r`   r9   r9   r9   r:   r]      s   r]   c                   s"   e  Z d  Z   f d d   Z   S)ModelFormMetaclassc                s;  d  } x< | D]4 } t  | d  r t  | j d  r | j j } Pq W| j d |  } t t |   j |  | | |  } | t f k r | St t	 | d d    } | _
 xl d d d g D][ }	 t	 | |	  }
 t |
 t  r |
 t k r d d | j d |	 d	 |
 i } t |   q W| j r%| j d  k rP| j d  k rPt d
 |   | j t k rhd  | _ t | j | j | j | j | | j | j | j | j | j d d 
} d d   | j   D } | j | j  } | rd } | d j |  | j j f } t |   | j  | j  n	 | j } | | _! | S)NMetarW   r%   r6   rX   zP%(model)s.Meta.%(opt)s cannot be a string. Did you mean to type: ('%(value)s',)?rU   optvaluez|Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form %s needs updating.rF   Fc             S   s"   h  |  ] \ } } | s |  q Sr9   r9   )rG   kvr9   r9   r:   	<setcomp>  s   	 z-ModelFormMetaclass.__new__.<locals>.<setcomp>z&Unknown field(s) (%s) specified for %sz, )"rB   rd   rW   popsuperrc   __new__r   r]   r?   r#   r'   strr   rR   rT   rU   r%   r6   r   r   rV   rX   rY   rZ   rL   r[   items
differenceZdeclared_fieldsjoinr   updateZbase_fields)Zmcsr)   basesattrsZbase_formfield_callbackbrW   	new_classr7   re   rf   msgr%   Znone_model_fieldsZmissing_fieldsmessage)	__class__r9   r:   rl      sR    !!		
				zModelFormMetaclass.__new__)rR   ra   rb   rl   r9   r9   )rx   r:   rc      s   rc   c                   s   e  Z d  Z d d d d d e d d d d d   f d d  Z d d   Z d d	   Z d
 d   Z d d   Z d d   Z	 d d   Z
 d d d  Z d e _   S)r   Nzid_%sFc                s   |  j  } | j d  k r$ t d   |	 d  k rH | j   |  _ i  } n! |	 |  _ t |	 | j | j  } | d  k	 r | j |  d |  _ t	   j
 | | | | | | | | d |
 d | x! |  j j   D] } t |  q Wd  S)Nz'ModelForm has no model class specified.Fuse_required_attributerenderer)r#   rU   
ValueErrorr5   r   r%   r6   rq   _validate_uniquerk   r`   valuesrE   )r^   r-   r.   auto_idprefixinitialerror_classZlabel_suffixZempty_permittedr5   ry   rz   r7   Zobject_datarC   )rx   r9   r:   r`     s"    					zBaseModelForm.__init__c             C   s%  g  } x|  j  j j D]} | j } | |  j k rG | j | j  q |  j j rx | |  j j k rx | j | j  q |  j j r | |  j j k r | j | j  q | |  j k r | j | j  q |  j | } |  j j |  } | j	 r | j
 r | | j k r | j | j  q W| S)z
        For backwards-compatibility, exclude several types of fields from model
        validation. See tickets #12507, #12521, #12553.
        )r5   r#   r%   r)   r2   r6   _errorsr$   r0   Zblankrequiredr1   )r^   r6   r8   r+   Z
form_fieldZfield_valuer9   r9   r:   _get_validation_exclusions5  s     		#z(BaseModelForm._get_validation_exclusionsc             C   s   d |  _  |  j S)NT)r|   r$   )r^   r9   r9   r:   clean^  s    	zBaseModelForm.cleanc             C   s   |  j  } t | d  r$ | j } n t | i } x | j   D] \ } } | t k r} | j r} t | j k r} | j t } n% | |  j k r= |  j | j } n q= x< | D]4 } t | t  r | j	 | k r | | j	 | _
 q Wq= W|  j d  |  d  S)N
error_dict)r#   rB   r   r   rn   rL   r%   r'   r   coderw   Z	add_error)r^   errorsr7   r   r+   messagesrL   rw   r9   r9   r:   _update_errorsb  s     	zBaseModelForm._update_errorsc             C   s  |  j  } |  j   } x9 |  j j   D]( \ } } t | t  r% | j |  q% Wy% t |  |  j | j | j	  |  _ Wn2 t
 k
 r } z |  j |  WYd  d  } ~ Xn Xy |  j j d | d d  Wn2 t
 k
 r } z |  j |  WYd  d  } ~ Xn X|  j r|  j   d  S)Nr6   validate_uniqueF)r#   r   r%   rn   r'   InlineForeignKeyFieldr2   r;   r5   r6   r   r   Z
full_cleanr|   r   )r^   r7   r6   r)   r+   er9   r9   r:   _post_clean~  s    		%  	zBaseModelForm._post_cleanc             C   s\   |  j    } y |  j j d |  Wn2 t k
 rW } z |  j |  WYd d } ~ Xn Xd S)z
        Call the instance's validate_unique() method and update the form's
        validation errors if any were raised.
        r6   N)r   r5   r   r   r   )r^   r6   r   r9   r9   r:   r     s
    zBaseModelForm.validate_uniquec             C   s   |  j  } |  j j } |  j j } |  j j } x t | j | j  D]q } t | d  s[ qC | rs | j	 | k rs qC | r | j	 | k r qC | j	 | k rC | j
 |  j | | j	  qC Wd S)zS
        Save the many-to-many fields and generic relations for this form.
        r3   N)r$   r#   r6   r%   r5   r   r>   r=   rB   r)   r3   )r^   r$   r6   r%   r7   r8   r9   r9   r:   	_save_m2m  s    	zBaseModelForm._save_m2mTc             C   sp   |  j  r= t d |  j j j |  j j j r0 d n d f   | r] |  j j   |  j   n |  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.
        z8The %s could not be %s because the data didn't validate.createdchanged)
r   r{   r5   r#   Zobject_name_stateaddingsaver   save_m2m)r^   commitr9   r9   r:   r     s    	"zBaseModelForm.save)rR   ra   rb   r   r`   r   r   r   r   r   r   r   alters_datar9   r9   )rx   r:   r     s   		)c               @   s   e  Z d  Z d S)r   N)rR   ra   rb   r9   r9   r9   r:   r     s   	metaclassc             C   sx  d |  i } | d k	 r" | | d <| d k	 r8 | | d <| d k	 rN | | d <| d k	 rd | | d <| d k	 rz | | d <| d k	 r | | d <|	 d k	 r |	 | d	 <|
 d k	 r |
 | d
 <t  | d  r | j f n f  } t d | |  } | rt |  | _ |  j d } d | d | i } t | d d  d k r_t | d d  d k r_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.
    rU   Nr%   r6   rV   rX   rY   rZ   rL   r[   rd   ZFormrW   zZCalling modelform_factory without defining 'fields' or 'exclude' explicitly is prohibited.)rB   rd   typestaticmethodrW   rR   r?   r   )rU   r4   r%   r6   rW   rV   rX   rY   rZ   rL   r[   rs   rr   rd   
class_nameZform_class_attrsr9   r9   r:   r      s:    '







!	c            
       sE  e  Z d  Z d Z d Z e   Z d d d d d d d   f d d Z   f d d   Z d	 d
   Z	 d d   Z
   f d d   Z d d   Z d d d  Z d d d  Z d d d  Z d d d  Z d e _ d d   Z d d   Z d d   Z d  d!   Z d" d#   Z d d$ d%  Z d d& d'  Z   f d( d)   Z   S)*r   zO
    A ``FormSet`` for editing a queryset and/or adding new objects to it.
    Nzid_%sr   c         	      sD   | |  _  | |  _ t   j d | d | d | d | i |    d  S)Nr-   r.   r~   r   )r@   initial_extrark   r`   )r^   r-   r.   r~   r   r@   r   r\   )rx   r9   r:   r`   8  s    		zBaseModelFormSet.__init__c                s&   |  j  s t |  j    St   j   S)z=Return the number of forms that are required in this FormSet.)is_boundlenget_querysetrk   initial_form_count)r^   )rx   r9   r:   r   >  s    	z#BaseModelFormSet.initial_form_countc             C   s;   t  |  d  s+ d d   |  j   D |  _ |  j j |  S)N_object_dictc             S   s   i  |  ] } | | j   q Sr9   )pk)rG   or9   r9   r:   rP   F  s   	 z5BaseModelFormSet._existing_object.<locals>.<dictcomp>)rB   r   r   r0   )r^   r   r9   r9   r:   _existing_objectD  s    z!BaseModelFormSet._existing_objectc             C   s,   x" | j  d k	 r$ | j  j   } q W| 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.
        N)remote_fieldZget_related_field	to_python)r^   r+   r9   r9   r:   _get_to_pythonI  s    zBaseModelFormSet._get_to_pythonc                sN  | |  j    k  } | r |  j r d |  j |  |  j j j j f } y |  j | } Wn t k
 rk Yq X|  j	 |  j j j  } y | |  } Wn t
 k
 r Yq X|  j |  | d <q|  j   | | d <n= |  j ry |  j | |  j    | d <Wn t k
 rYn Xt   j | |  } | rJd | j |  j j j j _ | S)Nz%s-%sr5   r   T)r   r   r/   rU   r#   r   r)   r-   KeyErrorr   r   r   r   r   
IndexErrorrk   _construct_formr%   r   )r^   ir\   Zpk_requiredZpk_keyr   r   r4   )rx   r9   r:   r   R  s0    	%	z BaseModelFormSet._construct_formc             C   sp   t  |  d  si |  j d  k	 r* |  j } n |  j j j   } | j s` | j |  j j j j	  } | |  _
 |  j
 S)N	_queryset)rB   r@   rU   _default_managerr   orderedZorder_byr#   r   r)   r   )r^   qsr9   r9   r:   r   t  s    		zBaseModelFormSet.get_querysetTc             C   s   | j  d |  S)z8Save and return a new model instance for the given form.r   )r   )r^   r4   r   r9   r9   r:   save_new  s    zBaseModelFormSet.save_newc             C   s   | j  d |  S)z>Save and return an existing model instance for the given form.r   )r   )r^   r4   r5   r   r9   r9   r:   save_existing  s    zBaseModelFormSet.save_existingc             C   s   | r | j    d S)z#Deletes an existing model instance.N)delete)r^   objr   r9   r9   r:   delete_existing  s    z BaseModelFormSet.delete_existingc                sD   | s* g    _    f d d   } |   _   j |    j |  S)z
        Save model instances for every form, adding and changing instances
        as necessary, and return the list of instances.
        c                 s"   x   j  D] }  |  j   q
 Wd  S)N)saved_formsr   )r4   )r^   r9   r:   r     s    z'BaseModelFormSet.save.<locals>.save_m2m)r   r   save_existing_objectssave_new_objects)r^   r   r   r9   )r^   r:   r     s
    		zBaseModelFormSet.savec             C   s   |  j    d  S)N)r   )r^   r9   r9   r:   r     s    zBaseModelFormSet.cleanc                s  t    } t    }  j   f d d    j D } xO | D]G     j   }   j j d |  \ } } | j |  | j |  q> Wg  } x | D] \ } }	 t    }
 x | D]      f d d   |	 D } t d d   | D  } | r d  | k r | |
 k rj| j  j	 |	    j
  j   g    j t <x' |	 D] } |   j k rG  j | =qGW|
 j |  q Wq Wx"| D]} t    }
 | \ } } } } x | D]     j r  j | d  k	 r  j | d  k	 r| d k r  j | } | j | j | j f } n t   j | |  f }   j | f | } | |
 k r| j  j |    j
  j   g    j t <  j | =|
 j |  qWqW| rt |   d  S)Nc                s.   g  |  ]$ } | j    r |   k r |  q Sr9   )Zis_valid)rG   r4   )forms_to_deleter9   r:   rI     s   	 z4BaseModelFormSet.validate_unique.<locals>.<listcomp>r6   c             3   s@   |  ]6 } |   j  k r |  j k r- | n
   j  | Vq d  S)N)r$   unique_fields)rG   r+   )r4   r^   r9   r:   	<genexpr>  s   z3BaseModelFormSet.validate_unique.<locals>.<genexpr>c             s   sK   |  ]A } t  | d   r$ | j   n t | t  r? t |  n | Vq d S)_get_pk_valN)rB   r   r'   listtuple)rG   dr9   r9   r:   r     s   date)setdeleted_formsZformsr   r5   Z_get_unique_checksrq   r   r2   get_unique_error_messager   get_form_errorr   r   r$   addyearmonthdayr?   get_date_error_messager   )r^   Zall_unique_checksZall_date_checksZvalid_formsr6   Zunique_checksZdate_checksr   Zuclassunique_checkZ	seen_dataZrow_datar+   
date_checklookupZ
unique_forr   Z	date_datar-   r9   )r4   r   r^   r:   r     sX    				
		
z BaseModelFormSet.validate_uniquec             C   sQ   t  |  d k r* t d  d | d i St d  d t | t d   i Sd  S)N   z0Please correct the duplicate data for %(field)s.r+   r   zFPlease correct the duplicate data for %(field)s, which must be unique.and)r   r   r   _)r^   r   r9   r9   r:   r     s
    		z)BaseModelFormSet.get_unique_error_messagec          	   C   s2   t  d  d | d d | d d t | d  i S)NzoPlease correct the duplicate data for %(field_name)s which must be unique for the %(lookup)s in %(date_field)s.
field_name   Z
date_field   r   r   )r   rm   )r^   r   r9   r9   r:   r     s
    

z'BaseModelFormSet.get_date_error_messagec             C   s
   t  d  S)Nz*Please correct the duplicate values below.)r   )r^   r9   r9   r:   r     s    zBaseModelFormSet.get_form_errorc             C   s   g  |  _  g  |  _ |  j s g  Sg  } |  j } x |  j D] } | j } | j d  k rY q8 | | k r |  j j |  |  j | d | q8 | j   r8 |  j  j | | j	 f  | j |  j
 | | d |  | s8 |  j j |  q8 W| S)Nr   )Zchanged_objectsZdeleted_objectsZinitial_formsr   r5   r   r2   r   has_changedZchanged_datar   r   )r^   r   Zsaved_instancesr   r4   r   r9   r9   r:   r     s&    					z&BaseModelFormSet.save_existing_objectsc             C   s   g  |  _  xp |  j D]e } | j   s( q |  j rC |  j |  rC q |  j  j |  j | d |  | s |  j j |  q W|  j  S)Nr   )Znew_objectsZextra_formsr   
can_deleteZ_should_delete_formr2   r   r   )r^   r   r4   r9   r9   r:   r   !  s    	z!BaseModelFormSet.save_new_objectsc       	         s  d d l  m   m } m } |  j j j |  _ }    f d d     |  se | j | j	 k r| j
 r | j j j r d n	 | j j } nG y, | d k	 r |  j   | j } n d } Wn t k
 r d } Yn Xt | | | 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 } t | d | d d d	 | | j	 |  j j <t   j | |  d S)
z0Add a hidden field for the object's primary key.r   )r(   OneToOneField
ForeignKeyc                sM   |  j  pL |  j pL t |     pL |  j oL |  j j oL  |  j j j j  S)N)r&   Zauto_createdr'   r   Zparent_linkrU   r#   r   )r   )r(   pk_is_not_editabler9   r:   r   9  s    "z7BaseModelFormSet.add_fields.<locals>.pk_is_not_editableNr   r   Fr,   )django.db.modelsr(   r   r   rU   r#   r   	_pk_fieldr)   r%   r   r5   r   r   r   r   r'   r   r   ZusingdbrV   r0   r   r   rk   
add_fields)	r^   r4   indexr   r   r   Zpk_valuer   r,   )rx   )r(   r   r:   r   /  s*    	$
!+zBaseModelFormSet.add_fields)rR   ra   rb   __doc__rU   r   r   r`   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r9   r9   )rx   r:   r   /  s.   		"	I

r   Fc             C   s   t  | d d  } t  | d |  d k rN t  | d |	  d k rN t d   t |  d | d | d |	 d | d |
 d	 | d
 | d | d | d | 
} t | | d | d | d | d | d | d | d | } |  | _ | S)z8Return a FormSet class for the given Django model class.rd   Nr%   r6   z]Calling modelformset_factory without defining 'fields' or 'exclude' explicitly is prohibited.r4   rW   rV   rX   rY   rZ   rL   r[   extramin_nummax_num	can_orderr   validate_minvalidate_max)r?   r   r    r   rU   )rU   r4   rW   formsetr   r   r   r   r%   r6   rV   r   rX   rY   rZ   rL   r   r   r[   metaFormSetr9   r9   r:   r   [  s    		c            	       s   e  Z d  Z d Z d d d d d d   f d d  Z   f d d   Z   f d d	   Z e d
 d    Z d   f d d  Z	   f d d   Z
   f d d   Z   S)r   z0A formset for child objects related to a parent.NFc       	         sA  | d  k r$ |  j  j j   |  _ n	 | |  _ | |  _ | d  k rN |  j j } |  j j d  k	 r | j |  j  j |  j i   } n | j	   } |  j  j h |  _
 t   j | | d | d | | |  j j j r=|  j  j |  j j j k r=t |  j j j t  r!t |  j j j  |  j j _ |  j j j j |  j  j  d  S)Nr   r@   )fkr   rU   r5   save_as_newr   r   filterr)   noner   rk   r`   r4   r#   r%   r'   r   r   r2   )	r^   r-   r.   r5   r   r   r@   r\   r   )rx   r9   r:   r`   z  s    		!"*zBaseInlineFormSet.__init__c                s   |  j  r d St   j   S)Nr   )r   rk   r   )r^   )rx   r9   r:   r     s    	z$BaseInlineFormSet.initial_form_countc                s  t    j | |  } |  j r t | j d d   } | d  k	 rK d | j _ d  | j | j |  j j  <d  | j | j |  j	 j  <| d  k	 r | | j _ |  j
 j } |  j	 j j |  j	 j j j j j k r t |  j
 |  j	 j j  } t | d |  } t | j
 |  j	 j   |  | S)N_mutableTr   )rk   r   r   r?   r-   r   r/   r   r)   r   r5   r   r   r   rU   r#   setattrZget_attname)r^   r   r\   r4   ZmutableZfk_value)rx   r9   r:   r     s    	'z!BaseInlineFormSet._construct_formc             C   s%   |  j  j j d |  j  j d d  S)NrU   + )r   r   Zget_accessor_namerU   replace)clsr9   r9   r:   get_default_prefix  s    z$BaseInlineFormSet.get_default_prefixTc                s2   t  | j |  j j |  j  t   j | d | S)Nr   )r   r5   r   r)   rk   r   )r^   r4   r   )rx   r9   r:   r     s    zBaseInlineFormSet.save_newc                sD  t    j | |  |  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 k r |  j j
 j | d <|  j j j r'| j d  d  k	 r |  j j j | d  } n |  j j j } | j   r't |  j | j d   t |  j |  | j | <d  S)Npk_fieldTrJ   to_field)rk   r   r   r   r)   r?   r%   r0   r   Zverbose_namer   r   rU   r#   r   r5   r   r   	get_fieldr*   r   Zattnamer   )r^   r4   r   r)   r\   r   )rx   r9   r:   r     s    0'zBaseInlineFormSet.add_fieldsc                s)     f d d   | D } t    j |  S)Nc                s(   g  |  ] } |   j  j k r |  q Sr9   )r   r)   )rG   r+   )r^   r9   r:   rI     s   	 z>BaseInlineFormSet.get_unique_error_message.<locals>.<listcomp>)rk   r   )r^   r   )rx   )r^   r:   r     s    z*BaseInlineFormSet.get_unique_error_message)rR   ra   rb   r   r`   r   r   classmethodr   r   r   r   r9   r9   )rx   r:   r   x  s   	c                sq  d d l  m   | j }  r  f d d   | j D } t |  d k r | d } t |    s | j j  k r | j j  j j   k r t	 d   j j
 f   qm| smt	 d | j j
  f   n    f d d   | j D } t |  d k r| d } nW | sK| r&d	 St	 d
 | j j
  j j
 f   n" t	 d | j j
  j j
 f   | S)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   )r   c                s%   g  |  ] } | j    k r |  q Sr9   )r)   )rG   r8   )fk_namer9   r:   rI     s   	 z$_get_foreign_key.<locals>.<listcomp>r   z)fk_name '%s' is not a ForeignKey to '%s'.z'%s' has no field named '%s'.c                sR   g  |  ]H } t  |    r | j j  k sH | j j  j j   k r |  q Sr9   )r'   r   rU   r#   get_parent_list)rG   r8   )r   parent_modelr9   r:   rI     s   	 Nz'%s' has no ForeignKey to '%s'.z*'%s' has more than one ForeignKey to '%s'.)r   r   r#   r%   r   r'   r   rU   r   r{   rJ   )r   rU   r   Zcan_failr7   Zfks_to_parentr   r9   )r   r   r   r:   _get_foreign_key  s:    		
		r   r   c          $   C   s   t  |  | d | } | j r$ d }
 d | d | d | d | d |	 d | d	 | d
 | d | d |
 d | d | d | d | d | d | d | d | i } t | |  } | | _ | 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   r4   rW   r   r   r   r   r%   r6   r   r   rV   r   r   rX   rY   rZ   rL   r[   )r   uniquer   r   )r   rU   r4   r   r   r%   r6   r   r   r   r   rW   rV   r   rX   rY   rZ   rL   r   r   r[   r   r\   r   r9   r9   r:   r     s0    		c                   sd   e  Z d  Z d Z e Z d e d  i Z d d d d   f d d	  Z d
 d   Z	 d d   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.r   Fr   Nc               s   | |  _  | |  _ | |  _ |  j  d  k	 r_ |  j rO t |  j  |  j  | d <n |  j  j | d <d | d <t   j | |   d  S)Nr   Fr   )parent_instancer   r   r?   r   rk   r`   )r^   r   r   r   argsr\   )rx   r9   r:   r`   I  s    				
zInlineForeignKeyField.__init__c             C   s   | |  j  k r# |  j r d  S|  j S|  j rD t |  j |  j  } n |  j j } t |  t |  k r t |  j d d d  |  j S)Nr   r   )	r1   r   r   r   r?   r   rm   r   rL   )r^   rf   origr9   r9   r:   r   U  s    		zInlineForeignKeyField.cleanc             C   s   d S)NFr9   )r^   r   r-   r9   r9   r:   r   d  s    z!InlineForeignKeyField.has_changed)rR   ra   rb   r   r   r,   r   default_error_messagesr`   r   r   r9   r9   )rx   r:   r   ?  s   r   c               @   sL   e  Z d  Z d d   Z d d   Z d d   Z d d   Z d	 d
   Z d S)ModelChoiceIteratorc             C   s   | |  _  | j |  _ d  S)N)r+   r@   )r^   r+   r9   r9   r:   r`   i  s    	zModelChoiceIterator.__init__c             c   sd   |  j  j d  k	 r# d |  j  j f V|  j } | j sA | j   } x | D] } |  j |  VqH Wd  S)Nr   )r+   empty_labelr@   Z_prefetch_related_lookupsiteratorchoice)r^   r@   r   r9   r9   r:   __iter__m  s    		zModelChoiceIterator.__iter__c             C   s)   |  j  j   |  j j d  k	 r$ d n d S)Nr   r   )r@   countr+   r   )r^   r9   r9   r:   __len__w  s    zModelChoiceIterator.__len__c             C   s   |  j  j d  k	 p |  j j   S)N)r+   r   r@   exists)r^   r9   r9   r:   __bool__}  s    zModelChoiceIterator.__bool__c             C   s"   |  j  j |  |  j  j |  f S)N)r+   prepare_valuelabel_from_instance)r^   r   r9   r9   r:   r    s    zModelChoiceIterator.choiceN)rR   ra   rb   r`   r  r  r  r  r9   r9   r9   r:   r   h  s
   
r   c                   s  e  Z d  Z d Z d e d  i Z e Z d d d d d d	 d
 d	 d d	 d d d d	 d d	 d d  Z d d   Z	   f d d   Z
 d d   Z d d   Z e e e  Z d d   Z d d   Z e e e j  Z   f d d   Z d  d!   Z d" d#   Z d$ d%   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.r   z	---------r   Tr,   NrJ   r   rK   r   to_field_namerD   c            K   st   | r | d  k	 r d  |  _  n	 | |  _  t j |  d | d | d | d | d | |
 | |  _ |	 |  _ | |  _ d  S)Nr   r,   rJ   r   rK   )r   r   r`   r@   rD   r
  )r^   r@   r   r   r,   rJ   r   rK   r
  rD   r\   r9   r9   r:   r`     s    			zModelChoiceField.__init__c             C   s    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.
        )rS   rD   )r^   r9   r9   r:   rA     s    
z%ModelChoiceField.get_limit_choices_toc                s=   t  t |   j |  } |  j d  k	 r9 |  j j   | _ | S)N)rk   r   __deepcopy__r@   all)r^   memoresult)rx   r9   r:   r    s    zModelChoiceField.__deepcopy__c             C   s   |  j  S)N)r   )r^   r9   r9   r:   _get_queryset  s    zModelChoiceField._get_querysetc             C   s4   | d  k r d  n	 | j    |  _ |  j |  j _ d  S)N)r  r   choicesr,   )r^   r@   r9   r9   r:   _set_queryset  s    !zModelChoiceField._set_querysetc             C   s
   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.
        )rm   )r^   r   r9   r9   r:   r	    s    z$ModelChoiceField.label_from_instancec             C   s#   t  |  d  r |  j S|  j |   S)N_choices)rB   r  r  )r^   r9   r9   r:   _get_choices  s    	zModelChoiceField._get_choicesc                s?   t  | d  r/ |  j r( | j |  j  S| j St   j |  S)Nr#   )rB   r
  Zserializable_valuer   rk   r  )r^   rf   )rx   r9   r:   r    s
    	zModelChoiceField.prepare_valuec             C   s   | |  j  k r d  SyO |  j p" d } t | |  j j  rI t | |  } |  j j | | i   } Wn= t t |  j j j	 f k
 r t
 |  j d d d  Yn X| S)Nr   r   r   )r1   r
  r'   r@   rU   r?   r0   r{   rT   ZDoesNotExistr   rL   )r^   rf   keyr9   r9   r:   r     s    zModelChoiceField.to_pythonc             C   s   t  j |  |  S)N)r   validate)r^   rf   r9   r9   r:   r    s    zModelChoiceField.validatec             C   s\   |  j  r d S| d  k	 r | n d } | d  k	 r7 | n d } t |  j |   t |  k S)NFr   )disabledrm   r  )r^   r   r-   Zinitial_valueZ
data_valuer9   r9   r:   r     s
    	zModelChoiceField.has_changed)rR   ra   rb   r   r   r   r   r  r`   rA   r  r  r  propertyr@   r	  r  r   Z_set_choicesr  r  r   r  r   r9   r9   )rx   r:   r     s$   	
c                   s   e  Z d  Z d Z e Z e Z d e d  d e d  d e d  i Z	   f d d	   Z
 d
 d   Z d d   Z d d   Z   f d d   Z d d   Z   S)r   z9A MultipleChoiceField whose choices are a model QuerySet.r   zEnter a list of values.r   zESelect a valid choice. %(value)s is not one of the available choices.invalid_pk_valueu"   “%(pk)s” is not a valid value.c                s   t    j | d d  | d  S)Nr   )rk   r`   )r^   r@   r\   )rx   r9   r:   r`      s    z!ModelMultipleChoiceField.__init__c             C   s   | s
 g  St  |  j |   S)N)r   _check_values)r^   rf   r9   r9   r:   r     s    z"ModelMultipleChoiceField.to_pythonc             C   s   |  j  |  } |  j r; | r; t |  j d d d  n |  j rY | rY |  j j   St | t t f  s t |  j d d d  |  j	 |  } |  j
 |  | S)Nr   r   r   )r  r   r   rL   r@   r   r'   r   r   r  Zrun_validators)r^   rf   r   r9   r9   r:   r     s    zModelMultipleChoiceField.cleanc                s9  |  j  p d   y t |  } Wn+ t k
 rL t |  j d d d  Yn Xxh | D]` } y |  j j   | i   WqT t t f k
 r t |  j d d d d d | i  YqT XqT W|  j j d   | i   }   f d d   | D } xE | D]= } t |  | k r t |  j d	 d d	 d d
 | i  q W| S)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.)
        r   r   r   r  paramsz%s__inc                s%   h  |  ] } t  t |      q Sr9   )rm   r?   )rG   r   )r  r9   r:   ri   1  s   	 z9ModelMultipleChoiceField._check_values.<locals>.<setcomp>r   rf   )	r
  	frozensetrT   r   rL   r@   r   r{   rm   )r^   rf   r   r   Zpksvalr9   )r  r:   r    s0    z&ModelMultipleChoiceField._check_valuesc                sb   t  | d  rR t | t  rR t  | d  rR t   j     f d d   | D St   j |  S)Nr  r#   c                s   g  |  ] }   |   q Sr9   r9   )rG   rh   )r  r9   r:   rI   @  s   	 z:ModelMultipleChoiceField.prepare_value.<locals>.<listcomp>)rB   r'   rm   rk   r  )r^   rf   )rx   )r  r:   r  ;  s    z&ModelMultipleChoiceField.prepare_valuec             C   s   |  j  r d S| d  k r g  } | d  k r1 g  } t |  t |  k rM d Sd d   |  j |  D } d d   | D } | | k S)NFTc             S   s   h  |  ] } t  |   q Sr9   )rm   )rG   rf   r9   r9   r:   ri   L  s   	 z7ModelMultipleChoiceField.has_changed.<locals>.<setcomp>c             S   s   h  |  ] } t  |   q Sr9   )rm   )rG   rf   r9   r9   r:   ri   M  s   	 )r  r   r  )r^   r   r-   Zinitial_setZdata_setr9   r9   r:   r   C  s    	z$ModelMultipleChoiceField.has_changed)rR   ra   rb   r   r   r,   r   Zhidden_widgetr   r   r`   r   r   r  r  r   r9   r9   )rx   r:   r     s   %c             C   s1   t  |  d  o0 |  j j d  k	 p0 |  j j d  k	 S)Nr#   )rB   r#   r%   r6   )rM   r9   r9   r:   modelform_defines_fieldsQ  s    r  )3r   	itertoolsr   Zdjango.core.exceptionsr   r   r   r   Zdjango.forms.fieldsr   r   Zdjango.forms.formsr	   r
   Zdjango.forms.formsetsr   r   Zdjango.forms.utilsr   Zdjango.forms.widgetsr   r   r   Zdjango.utils.textr   r   Zdjango.utils.translationr   r   r   r!   r   r;   r   rE   r   r]   rc   r   r   r    r   r   r   r   r   r   r   r   r   r  r9   r9   r9   r:   <module>   sf   "		)		VH			R -			d7			&)q\