
0_c           @  s<  d  Z  d d l m Z d d l 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 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 d d l 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' e( e( d  Z) e( e( d  Z* d   Z+ e( e( e( e( e( e( e( e( e( e, d 
 Z- d  e. f d!     YZ/ d" e f d#     YZ0 d$ e f d%     YZ1 d& e j2 e0 e1  f d'     YZ3 e3 e( e( e( e( e( e( e( e( e( d( 
 Z4 d) e f d*     YZ5 e3 e( e5 d+ e6 e6 e( e( e( e( e6 e( e( e( e( e( e6 e( d,  Z7 d- e5 f d.     YZ8 e( e6 d/  Z9 e3 e8 e( e( e( d0 e6 e, e( e( e( e6 e( e( e( e( e( e6 e( d1  Z: d2 e f d3     YZ; d4 e. f d5     YZ< d6 e f d7     YZ= d8 e= f d9     YZ> d:   Z? d; S(<   u[   
Helper functions for creating Form classes from Django models
and database field objects.
i(   t   unicode_literals(   t   OrderedDict(   t   chain(   t   NON_FIELD_ERRORSt
   FieldErrort   ImproperlyConfiguredt   ValidationError(   t   ChoiceFieldt   Field(   t   BaseFormt   DeclarativeFieldsMetaclass(   t   BaseFormSett   formset_factory(   t	   ErrorList(   t   HiddenInputt   MultipleHiddenInputt   SelectMultiple(   t   six(   t
   force_text(   t   capfirstt   get_text_list(   t   ugettextt   ugettext_lazyu	   ModelFormu   BaseModelFormu   model_to_dictu   fields_for_modelu   ModelChoiceFieldu   ModelMultipleChoiceFieldu
   ALL_FIELDSu   BaseModelFormSetu   modelformset_factoryu   BaseInlineFormSetu   inlineformset_factoryu   modelform_factoryu   __all__c   	      C  sT  d d l  m } | j } |  j } g  } x | j D] } | j s2 t | | j  s2 | j | k ri q2 n  | d k	 r | j | k r q2 n  | r | j | k r q2 n  | j
   r |  | j j j j |  j |  j |  j | j   r q2 n  t | | j  r| j |  q2 | j | | | j  q2 Wx% | D] } | j | | | j  q/W| S(   u   
    Constructs and returns a model instance from the bound ``form``'s
    ``cleaned_data``, but does not save the returned instance to the
    database.
    i(   t   modelsN(   t	   django.dbR   t   _metat   cleaned_datat   fieldst   editablet
   isinstancet	   AutoFieldt   namet   Nonet   has_defaultt   fieldt   widgett   value_omitted_from_datat   datat   filest
   add_prefixt	   FileFieldt   appendt   save_form_data(	   t   formt   instanceR   t   excludeR   t   optsR   t   file_field_listt   f(    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyt   construct_instance$   s*    		4c         C  s   d d l  m } |  j } i  } x t | j | j | j  D] } t | d t  sY q; n  | rt | j	 | k rt q; n  | r | j	 | k r q; n  | j
 |   | | j	 <t | | j  r; t | | j	  | | j	 <q; q; W| S(   u  
    Returns 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, only the named
    fields will be included in the returned dict.

    ``exclude`` is an optional list of field names. If provided, the named
    fields will be excluded from the returned dict, even if they are listed in
    the ``fields`` argument.
    i(   R   u   editable(   R   R   R   R   t   concrete_fieldst   private_fieldst   many_to_manyt   getattrt   FalseR   t   value_from_objectR   t   ManyToManyFieldt   list(   R,   R   R-   R   R.   R%   R0   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyt   model_to_dictK   s    	"!c         C  sU   t  |  d  rQ t  |  d  rQ |  j   } | d k	 rQ |  j j |  |  _ qQ n  d S(   u=   Apply limit_choices_to to the formfield's queryset if needed.u   querysetu   get_limit_choices_toN(   t   hasattrt   get_limit_choices_toR    t   querysett   complex_filter(   t	   formfieldt   limit_choices_to(    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyt#   apply_limit_choices_to_to_formfieldi   s    c         C  s  g  } g  } |  j  } d d l m } g  | j D] } t | |  r/ | ^ q/ } xZt t | j | | j   D]:} t	 | d t
  s | d k	 ro | j | k ro | d k s | j | k ro t d | j |  j f   qo qo n  | d k	 r| j | k rqo n  | r| j | k rqo n  i  } | rM| j | k rM| | j | d <n  | t k sn| r{| j | k r{t | d <n  | r| j | k r| | j | d <n  | r| j | k r| | j | d <n  | r| j | k r| | j | d	 <n  |	 r| j |	 k r|	 | j | d
 <n  | d k r=| j |   } n* t |  sXt d   n | | |  } | r|
 rt |  n  | j | j | f  qo | j | j  qo Wt |  } | rt g  | D]@ } | s| r| | k r| | k r| | j |  f ^ q } n  | S(   ud  
    Returns a ``OrderedDict`` containing form fields for the given model.

    ``fields`` is an optional list of field names. If provided, only the named
    fields will be included in the returned fields.

    ``exclude`` is an optional list of field names. If provided, the named
    fields will be excluded 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.
    i(   R   u   editableuH   '%s' cannot be specified for %s model form as it is a non-editable fieldu   widgetu   localizeu   labelu	   help_textu   error_messagesu
   form_classu1   formfield_callback must be a function or callableN(   R   t   django.db.models.fieldsR   R3   R   t   sortedR   R2   R4   R5   R6   R    R   R   t   __name__t
   ALL_FIELDSt   TrueR?   t   callablet	   TypeErrorRA   R)   R   t   get(   t   modelR   R-   t   widgetst   formfield_callbackt   localized_fieldst   labelst
   help_textst   error_messagest   field_classest   apply_limit_choices_tot
   field_listt   ignoredR.   t
   ModelFieldR0   t   sortable_private_fieldst   kwargsR?   t
   field_dict(    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyt   fields_for_modelq   s\    "	+%!Ft   ModelFormOptionsc           B  s   e  Z d d   Z RS(   c         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(
   Nu   modelu   fieldsu   excludeu   widgetsu   localized_fieldsu   labelsu
   help_textsu   error_messagesu   field_classes(   R5   R    RJ   R   R-   RK   RM   RN   RO   RP   RQ   (   t   selft   options(    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyt   __init__   s    N(   RD   t
   __module__R    R]   (    (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyRZ      s   t   ModelFormMetaclassc           B  s   e  Z d    Z RS(   c         C  sr  d  } x? | D]7 } t | d  r t | j d  r | j j } Pq q W| j d |  } t t |   j |  | | |  } | t f k r | St	 t
 | d d    } | _ xu d d d g D]d }	 t
 | |	  }
 t |
 t j  r |
 t k r d i | j d 6|	 d 6|
 d	 6} t |   q q W| j r\| j d  k r_| j d  k r_t d
 |   n  | j t k rzd  | _ n  t | j | j | j | j | | j | j | j | j | j d t 
} g  t j |  D] \ } } | s| ^ q} t |  t | j  j!    } | rId } | d j" |  | j j f } t# |   n  | j$ | j   n	 | j  } | | _% | S(   Nu   Metau   formfield_callbacku   fieldsu   excludeu   localized_fieldsuP   %(model)s.Meta.%(opt)s cannot be a string. Did you mean to type: ('%(value)s',)?u   modelu   optu   valueu|   Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form %s needs updating.RR   u&   Unknown field(s) (%s) specified for %su   , (&   R    R;   t   MetaRL   t   popt   superR_   t   __new__t   BaseModelFormRZ   R5   R   R   R   t   string_typesRE   RD   RH   RJ   R   R-   R   RY   RK   RM   RN   RO   RP   RQ   R6   t	   iteritemst   sett   declared_fieldst   keyst   joinR   t   updatet   base_fields(   t   mcsR   t   basest   attrst   base_formfield_callbackt   bRL   t	   new_classR.   t   optt   valuet   msgR   t   kt   vt   none_model_fieldst   missing_fieldst   message(    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyRc      sV    !!
		.			(   RD   R^   Rc   (    (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR_      s   Rd   c           B  sz   e  Z d	 d	 d  d	 d	 e d	 e d	 d	 d 
 Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z e d  Z e e _ RS(
   u   id_%sc         C  s   |  j  } | j d  k r' t d   n  |	 d  k rK | j   |  _ i  } n! |	 |  _ t |	 | j | j  } | d  k	 r | j |  n  t	 |  _
 t t |   j | | | | | | | | d |
 x! |  j j   D] } t |  q Wd  S(   Nu'   ModelForm has no model class specified.t   use_required_attribute(   R   RJ   R    t
   ValueErrorR,   R:   R   R-   Rk   R6   t   _validate_uniqueRb   Rd   R]   t   valuesRA   (   R[   R%   R&   t   auto_idt   prefixt   initialt   error_classt   label_suffixt   empty_permittedR,   R{   R.   t   object_dataR?   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR]   "  s     				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 j   k r | j | j  q |  j | } |  j j	 |  } | j
 r | j r | | j k r | j | j  q q W| S(   u   
        For backwards-compatibility, several types of fields need to be
        excluded from model validation. See the following tickets for
        details: #12507, #12521, #12553
        (   R,   R   R   R   R)   R-   t   _errorsRi   R   RI   t   blankt   requiredt   empty_values(   R[   R-   R0   R"   t
   form_fieldt   field_value(    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyt   _get_validation_exclusions=  s     		#c         C  s   t  |  _ |  j S(   N(   RF   R}   R   (   R[   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyt   cleang  s    	c         C  s   |  j  } t | d  r$ | j } n i | t 6} x | j   D] \ } } | t k r~ | j r~ t | j k r~ | j t } n% | |  j k r> |  j | j } n q> x? | D]7 } t | t  r | j	 | k r | | j	 | _
 q q Wq> W|  j d  |  d  S(   Nu
   error_dict(   R   R;   t
   error_dictR   t   itemsRP   R   R   R   t   codeRz   t	   add_errorR    (   R[   t   errorsR.   R   R"   t   messagesRP   Rz   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyt   _update_errorsk  s     	c         C  s   |  j  } |  j   } x< |  j j   D]+ \ } } t | t  r% | j |  q% q% Wy% t |  |  j | j | j	  |  _ Wn  t
 k
 r } |  j |  n Xy |  j j d | d t  Wn  t
 k
 r } |  j |  n X|  j r |  j   n  d  S(   NR-   t   validate_unique(   R   R   R   R   R   t   InlineForeignKeyFieldR)   R1   R,   R-   R   R   t
   full_cleanR6   R}   R   (   R[   R.   R-   R   R"   t   e(    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyt   _post_clean  s    		%	c         C  sJ   |  j    } y |  j j d |  Wn  t k
 rE } |  j |  n Xd S(   u   
        Calls the instance's validate_unique() method and updates the form's
        validation errors if any were raised.
        R-   N(   R   R,   R   R   R   (   R[   R-   R   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR     s
    c         C  s   |  j  } |  j j } |  j j } |  j j } x t | j | j  D]} } t | d  s^ qC n  | ry | j	 | k ry qC n  | r | j	 | k r qC n  | j	 | k rC | j
 |  j | | j	  qC qC Wd S(   uS   
        Save the many-to-many fields and generic relations for this form.
        u   save_form_dataN(   R   R   R-   R   R,   R   R4   R3   R;   R   R*   (   R[   R   R-   R   R.   R0   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyt	   _save_m2m  s    	c         C  ss   |  j  r@ t d |  j j j |  j j j r0 d n d f   n  | r` |  j j   |  j   n |  j |  _	 |  j S(   u   
        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.
        u8   The %s could not be %s because the data didn't validate.u   createdu   changed(
   R   R|   R,   R   t   object_namet   _statet   addingt   saveR   t   save_m2m(   R[   t   commit(    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR     s    	%N(   RD   R^   R    R   R6   R]   R   R   R   R   R   R   RF   R   t   alters_data(    (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyRd   !  s   		*					t	   ModelFormc           B  s   e  Z RS(    (   RD   R^   (    (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR     s   c         C  s  i |  d 6} | d k	 r& | | d <n  | d k	 r? | | d <n  | d k	 rX | | d <n  | d k	 rq | | d <n  | d k	 r | | d <n  | d k	 r | | d <n  |	 d k	 r |	 | d <n  |
 d k	 r |
 | d	 <n  t f } t | d
  r | j t f } n  t t d
  | |  } | r/t |  | _ n  |  j t d  } i | d
 6| d 6} t	 | d d  d k rt	 | d d  d k rt
 d   n  t |  | | f |  S(   u  
    Returns a ModelForm containing form fields for the given model.

    ``fields`` is an optional list of field names. If provided, only the named
    fields will be included in the returned fields. If omitted or '__all__',
    all fields will be used.

    ``exclude`` is an optional list of field names. If provided, the named
    fields will be excluded 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.
    u   modelu   fieldsu   excludeu   widgetsu   localized_fieldsu   labelsu
   help_textsu   error_messagesu   field_classesu   Metau   Formu   formfield_callbackuZ   Calling modelform_factory without defining 'fields' or 'exclude' explicitly is prohibited.N(   R    t   objectR;   R`   t   typet   strt   staticmethodRL   RD   R5   R   (   RJ   R+   R   R-   RL   RK   RM   RN   RO   RP   RQ   Ro   t   parentR`   t
   class_namet   form_class_attrs(    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyt   modelform_factory  s@    %	
t   BaseModelFormSetc           B  s   e  Z d  Z d Z e   Z d d d d d d  Z d   Z d   Z	 d   Z
 d   Z d   Z e d  Z e d	  Z e d
  Z e d  Z e e _ d   Z d   Z d   Z d   Z d   Z e d  Z e d  Z d   Z RS(   uO   
    A ``FormSet`` for editing a queryset and/or adding new objects to it.
    u   id_%sc         K  sg   | |  _  | j d d   |  _ i | d 6| d 6| d 6| d 6} | j |  t t |   j |   d  S(   Nu   initialu   datau   filesu   auto_idu   prefix(   R=   Ra   R    t   initial_extraRk   Rb   R   R]   (   R[   R%   R&   R   R   R=   RW   t   defaults(    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR]   A  s
    	"c         C  s5   |  j  p |  j s" t |  j    St t |   j   S(   u>   Returns the number of forms that are required in this FormSet.(   R%   R&   t   lent   get_querysetRb   R   t   initial_form_count(   R[   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR   I  s    c         C  s;   t  |  d  s+ d   |  j   D |  _ n  |  j j |  S(   Nu   _object_dictc         S  s   i  |  ] } | | j   q S(    (   t   pk(   t   .0t   o(    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pys
   <dictcomp>Q  s   	 (   R;   R   t   _object_dictRI   (   R[   R   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyt   _existing_objectO  s    c         C  s,   x" | j  d k	 r$ | j  j   } q W| j S(   u   
        If the field is a related field, fetch the concrete field's (that
        is, the ultimate pointed-to field's) to_python.
        N(   t   remote_fieldR    t   get_related_fieldt	   to_python(   R[   R"   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyt   _get_to_pythonT  s    c         K  s,  |  j  r | |  j   k  r d |  j |  |  j j j j f } |  j | } |  j j j } |  j |  } | |  } |  j	 |  | d <n  | |  j   k  r d | k r |  j
   | | d <n  | |  j   k r|  j ry |  j | |  j   | d <Wqt k
 rqXn  t t |   j | |  S(   Nu   %s-%su   instanceu   initial(   t   is_boundR   R'   RJ   R   R   R   R%   R   R   R   R   t
   IndexErrorRb   R   t   _construct_form(   R[   t   iRW   t   pk_keyR   t   pk_fieldR   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR   ]  s    %c         C  sv   t  |  d  so |  j d  k	 r* |  j } n |  j j j   } | j sc | j |  j j j	 j
  } n  | |  _ n  |  j S(   Nu	   _queryset(   R;   R=   R    RJ   t   _default_managerR   t   orderedt   order_byR   R   R   t	   _queryset(   R[   t   qs(    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR   o  s    	c         C  s   | j  d |  S(   u:   Saves and returns a new model instance for the given form.R   (   R   (   R[   R+   R   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyt   save_new  s    c         C  s   | j  d |  S(   u@   Saves and returns an existing model instance for the given form.R   (   R   (   R[   R+   R,   R   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyt   save_existing  s    c         C  s   | r | j    n  d S(   u#   Deletes an existing model instance.N(   t   delete(   R[   t   objR   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyt   delete_existing  s    c           sD   | s* g    _    f d   } |   _ n    j |    j |  S(   u   Saves model instances for every form, adding and changing instances
        as necessary, and returns the list of instances.
        c            s"   x   j  D] }  |  j   q
 Wd  S(   N(   t   saved_formsR   (   R+   (   R[   (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR     s    (   R   R   t   save_existing_objectst   save_new_objects(   R[   R   R   (    (   R[   s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR     s
    	c         C  s   |  j    d  S(   N(   R   (   R[   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR     s    c           s  t    } t    }  j } g   j D]$     j   r%   | k r%   ^ q% } x_ | D]W     j   }   j j d |  \ } } | j t  |   } | j t  |   } qV Wg  } x | D] \ }	 }
 t    } x | D]      f d   |
 D } t d   | D  } | r d  | k r | | k r| j
  j |
    j  j   g    j t <x- |
 D]" } |   j k ri  j | =qiqiWn  | j |  q q Wq Wx(| D] } t    } | \ }	 } } } x | D]     j r  j | d  k	 r  j | d  k	 r| d k rB  j | } | j | j | j f } n t   j | |  f }   j | f | } | | k r| j
  j |    j  j   g    j t <  j | =n  | j |  qqWqW| rt |   n  d  S(   NR-   c         3  s@   |  ]6 } |   j  k r |  j k r- | n
   j  | Vq d  S(   N(   R   t   unique_fields(   R   R"   (   R+   R[   (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pys	   <genexpr>  s   c         s  s0   |  ]& } t  | d   r$ | j   n | Vq d S(   u   _get_pk_valN(   R;   t   _get_pk_val(   R   t   d(    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pys	   <genexpr>  s   u   date(   Rg   t   deleted_formst   formst   is_validR   R,   t   _get_unique_checkst   uniont   tupleR    R)   t   get_unique_error_messageR   t   get_form_errorR   R   R   t   addt   yeart   montht   dayR5   t   get_date_error_messageR   (   R[   t   all_unique_checkst   all_date_checkst   forms_to_deletet   valid_formsR-   t   unique_checkst   date_checksR   t   uclasst   unique_checkt	   seen_datat   row_dataR"   t
   date_checkt   lookupt
   unique_fort   datet	   date_dataR%   (    (   R+   R[   s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR     sV    			4	
		c         C  s\   t  |  d k r+ t d  i | d d 6St d  i t | t j t d    d 6Sd  S(   Ni   u0   Please correct the duplicate data for %(field)s.i    u   fielduF   Please correct the duplicate data for %(field)s, which must be unique.u   and(   R   R   R   R   t	   text_typet   _(   R[   R   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR     s
    c         C  s8   t  d  i | d d 6| d d 6t j | d  d 6S(   Nuo   Please correct the duplicate data for %(field_name)s which must be unique for the %(lookup)s in %(date_field)s.i   u
   field_namei   u
   date_fieldi   u   lookup(   R   R   R   (   R[   R   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR     s    c         C  s
   t  d  S(   Nu*   Please correct the duplicate values below.(   R   (   R[   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR     s    c         C  s   g  |  _  g  |  _ |  j s g  Sg  } |  j } x |  j D] } | j } | j d  k r\ q8 n  | | k r |  j j |  |  j | d | q8 | j	   r8 |  j  j | | j
 f  | j |  j | | d |  | s |  j j |  q q8 q8 W| S(   NR   (   t   changed_objectst   deleted_objectst   initial_formsR   R,   R   R    R)   R   t   has_changedt   changed_dataR   R   (   R[   R   t   saved_instancesR   R+   R   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR     s&    					c         C  s   g  |  _  xy |  j D]n } | j   s+ q n  |  j rI |  j |  rI q n  |  j  j |  j | d |  | s |  j j |  q q W|  j  S(   NR   (   t   new_objectst   extra_formsR   t
   can_deletet   _should_delete_formR)   R   R   (   R[   R   R+   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR     s    	c   	        s  d d l  m   m } m } |  j j j |  _ }    f d     |  sb | j | j	 k r| j
 r | j j j r d n	 | j j } nF y, | d k	 r |  j   | j } n d } Wn t k
 r d } n Xt | |  s t | |  r| j j j j   } n |  j j j   } | j | j j j  } | j j rb| j j j |  j j t  } n t } t | d | d t d | | j	 |  j j <n  t t |   j | |  d S(   u0   Add a hidden field for the object's primary key.i(   R   t   OneToOneFieldt
   ForeignKeyc           sM   |  j  pL |  j pL t |     pL |  j oL |  j j oL  |  j j j j  S(   N(   R   t   auto_createdR   R   t   parent_linkRJ   R   R   (   R   (   R   t   pk_is_not_editable(    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR   /  s    "R   R   R#   N(   t   django.db.modelsR   R   R   RJ   R   R   t	   _pk_fieldR   R   R   R,   R   R   R    R   R   R   R   R   t   usingt   dbRK   RI   R   t   ModelChoiceFieldR6   Rb   R   t
   add_fields(	   R[   R+   t   indexR   R   R   t   pk_valueR   R#   (    (   R   R   s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR  %  s*    	$

!.N(   RD   R^   t   __doc__R    RJ   Rg   R   R]   R   R   R   R   R   RF   R   R   R   R   R   R   R   R   R   R   R   R   R  (    (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR   8  s.   										E	
	
	i   c         C  s   t  | d d  } t  | d |  d k rQ t  | d |	  d k rQ t d   n  t |  d | d | d |	 d | d	 |
 d
 | d | d | d | d | 
} t | | d | d | d | d | d | d | d | } |  | _ | S(   uC   
    Returns a FormSet class for the given Django model class.
    u   Metau   fieldsu   excludeu]   Calling modelformset_factory without defining 'fields' or 'exclude' explicitly is prohibited.R+   R   R-   RL   RK   RM   RN   RO   RP   RQ   t   extrat   min_numt   max_numt	   can_orderR   t   validate_mint   validate_maxN(   R5   R    R   R   R   RJ   (   RJ   R+   RL   t   formsetR  R   R	  R  R   R-   RK   R  RM   RN   RO   RP   R  R
  RQ   t   metat   FormSet(    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyt   modelformset_factoryQ  s    		t   BaseInlineFormSetc           B  sh   e  Z d  Z d d d e d d d  Z d   Z d   Z e d    Z	 e
 d  Z d   Z d   Z RS(	   u0   A formset for child objects related to a parent.c   	      K  sQ  | d  k r$ |  j j j   |  _ n	 | |  _ | |  _ | d  k rQ |  j j } n  |  j j d  k	 r | j i |  j |  j j	 6  } n | j
   } |  j j	 h |  _ t t |   j | | d | d | | |  j j j rM|  j j	 |  j j j k rMt |  j j j t  r.t |  j j j  |  j j _ n  |  j j j j |  j j	  n  d  S(   NR   R=   (   R    t   fkR   RJ   R,   t   save_as_newR   R   t   filterR   t   noneR   Rb   R  R]   R+   R   R   R   R   R9   R)   (	   R[   R%   R&   R,   R  R   R=   RW   R   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR]   r  s     		"
*!c         C  s    |  j  r d St t |   j   S(   Ni    (   R  Rb   R  R   (   R[   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR     s    	c         K  s-  t  t |   j | |  } |  j r t | j d d   } | d  k	 rT t | j _ n  d  | j | j	 |  j
 j  <d  | j | j	 |  j j  <| d  k	 r | | j _ q n  |  j j } |  j j j |  j j j j j j k rt |  j |  j j j  } t | d |  } n  t | j |  j j   |  | S(   Nu   _mutableu   pk(   Rb   R  R   R  R5   R%   R    RF   t   _mutableR'   R   R   R  R,   R   R   t
   field_nameRJ   R   t   setattrt   get_attname(   R[   R   RW   R+   t   mutablet   fk_value(    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR     s    	'c         C  s%   |  j  j j d |  j  j d d  S(   NRJ   u   +u    (   R  R   t   get_accessor_nameRJ   t   replace(   t   cls(    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyt   get_default_prefix  s    c         C  s   t  | j |  j j |  j  | j d t  } t |  j |  j j j  } t  | |  j j	   t | d |   | r | j   n  | r t
 | d  r | j   n  | S(   NR   u   pku   save_m2m(   R  R,   R  R   R   R6   R5   R   R  R  R;   R   (   R[   R+   R   R   R  (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR     s    %c         C  sU  t  t |   j | |  |  j |  j k rG |  j j } i t d 6} nz |  j j } i t | j j	 |  d t
 |  j j   d 6} |  j j j |  j j j j j j k r |  j j j | d <n  |  j j j r8| j	 d  d  k	 r|  j j j | d  } n |  j j j } | j   r8t |  j | j d   q8n  t |  j |  | j | <d  S(   Nu   pk_fieldu   labelu   to_field(   Rb   R  R  R   R  R   RF   R5   R   RI   R   t   verbose_nameR   R  RJ   R   R   R,   R   R   R    t	   get_fieldR!   R  t   attnameR   (   R[   R+   R  R   RW   t   to_field(    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR    s     .'c         C  sA   g  | D] } | |  j  j k r | ^ q } t t |   j |  S(   N(   R  R   Rb   R  R   (   R[   R   R"   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR     s    +N(   RD   R^   R  R    R6   R]   R   R   t   classmethodR  RF   R   R  R   (    (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR  p  s   				c   	      C  s  d d l  m } | j } | r g  | j D] } | j | k r) | ^ q) } t |  d k r | d } t | |  s | j j |  k r | j j |  j j	   k r t
 d | |  j j f   q qt |  d k rt
 d | j j | f   qn g  | j D]H } t | |  r| j j |  k sE| j j |  j j	   k r| ^ q} t |  d k rp| d } nc t |  d k r| rd St
 d | j j |  j j f   n" t
 d	 | j j |  j j f   | S(
   uI  
    Finds and returns the ForeignKey from model to parent if there is one
    (returns 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, an exception is raised if there is no ForeignKey from model to
    parent_model.
    i(   R   i   i    u)   fk_name '%s' is not a ForeignKey to '%s'.u   '%s' has no field named '%s'.Nu   '%s' has no ForeignKey to '%s'.u*   '%s' has more than one ForeignKey to '%s'.(   R   R   R   R   R   R   R   R   RJ   t   get_parent_listR|   t   label(	   t   parent_modelRJ   t   fk_namet   can_failR   R.   R0   t   fks_to_parentR  (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyt   _get_foreign_key  s@    		+
'		i   c         C  s   t  |  | d | } | j r' d }
 n  i | d 6| d 6| d 6| d 6|	 d 6| d 6| d	 6| d
 6| d 6|
 d 6| d 6| d 6| d 6| d 6| d 6| d 6| d 6| d 6} t | |  } | | _ | S(   u   
    Returns an ``InlineFormSet`` for the given kwargs.

    You must provide ``fk_name`` if ``model`` has more than one ``ForeignKey``
    to ``parent_model``.
    R'  i   u   formu   formfield_callbacku   formsetu   extrau
   can_deleteu	   can_orderu   fieldsu   excludeu   min_numu   max_numu   widgetsu   validate_minu   validate_maxu   localized_fieldsu   labelsu
   help_textsu   error_messagesu   field_classes(   R*  t   uniqueR  R  (   R&  RJ   R+   R  R'  R   R-   R  R	  R   R  RL   RK   R  RM   RN   RO   RP   R  R
  RQ   R  RW   R  (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyt   inlineformset_factory  s2    		
	R   c           B  sB   e  Z d  Z e Z i e d  d 6Z d   Z d   Z d   Z	 RS(   uw   
    A basic integer field that deals with validating the given value to a
    given parent instance in an inline.
    uE   The inline foreign key did not match the parent instance primary key.u   invalid_choicec         O  s   | |  _  | j d t  |  _ | j d d   |  _ |  j  d  k	 rz |  j rg t |  j  |  j  | d <qz |  j  j | d <n  t | d <t t	 |   j
 | |   d  S(   Nu   pk_fieldu   to_fieldu   initialu   required(   t   parent_instanceRa   R6   R   R    R"  R5   R   Rb   R   R]   (   R[   R-  t   argsRW   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR]   H  s    		
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  n  |  j S(   Nu   invalid_choiceR   (
   R   R   R    R-  R"  R5   R   R   R   RP   (   R[   Rt   t   orig(    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR   T  s    		c         C  s   t  S(   N(   R6   (   R[   R   R%   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR   c  s    (
   RD   R^   R  R   R#   R   t   default_error_messagesR]   R   R   (    (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR   >  s   		t   ModelChoiceIteratorc           B  s,   e  Z d    Z d   Z d   Z d   Z RS(   c         C  s   | |  _  | j |  _ d  S(   N(   R"   R=   (   R[   R"   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR]   h  s    	c         c  sp   |  j  j d  k	 r& d |  j  j f Vn  |  j j   } | j sM | j   } n  x | D] } |  j |  VqT Wd  S(   Nu    (   R"   t   empty_labelR    R=   t   allt   _prefetch_related_lookupst   iteratort   choice(   R[   R=   R   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyt   __iter__l  s    	c         C  s)   t  |  j  |  j j d  k	 r$ d n d S(   Ni   i    (   R   R=   R"   R2  R    (   R[   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyt   __len__v  s    c         C  s"   |  j  j |  |  j  j |  f S(   N(   R"   t   prepare_valuet   label_from_instance(   R[   R   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR6  y  s    (   RD   R^   R]   R7  R8  R6  (    (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR1  g  s   		
	R  c        	   B  s   e  Z d  Z i e d  d 6Z e Z d e d d d d d d d  Z	 d   Z
 d   Z d   Z d	   Z e e e  Z d
   Z d   Z e e e j  Z d   Z d   Z d   Z d   Z RS(   u1   A ChoiceField whose choices are a model QuerySet.uG   Select a valid choice. That choice is not one of the available choices.u   invalid_choiceu	   ---------u    c
      	   O  sh   | r | d  k	 r d  |  _ n	 | |  _ t j |  | | | | | |
 |  | |  _ |	 |  _ | |  _ d  S(   N(   R    R2  R   R]   R=   R@   t   to_field_name(   R[   R=   R2  R   R#   R%  R   t	   help_textR;  R@   R.  RW   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR]     s    	
		c         C  s    t  |  j  r |  j   S|  j S(   u   
        Returns ``limit_choices_to`` for this form field.

        If it is a callable, it will be invoked and the result will be
        returned.
        (   RG   R@   (   R[   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR<     s    
c         C  s@   t  t |   j |  } |  j d  k	 r< |  j j   | _ n  | S(   N(   Rb   R   t   __deepcopy__R=   R    R3  (   R[   t   memot   result(    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR=    s    c         C  s   |  j  S(   N(   R   (   R[   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyt   _get_queryset  s    c         C  s   | |  _  |  j |  j _ d  S(   N(   R   t   choicesR#   (   R[   R=   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyt   _set_queryset  s    	c         C  s
   t  |  S(   u   
        This method is used to convert objects into strings; it's used to
        generate the labels for the choices presented by this object. Subclasses
        can override this method to customize the display of the choices.
        (   R   (   R[   R   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR:    s    c         C  s#   t  |  d  r |  j S|  j |   S(   Nu   _choices(   R;   t   _choicesR5  (   R[   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyt   _get_choices  s    	c         C  sH   t  | d  r2 |  j r( | j |  j  S| j Sn  t t |   j |  S(   Nu   _meta(   R;   R;  t   serializable_valueR   Rb   R  R9  (   R[   Rt   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR9    s
    	
c         C  s   | |  j  k r d  Sy, |  j p" d } |  j j i | | 6  } Wn< t t |  j j j f k
 r} t	 |  j
 d d d  n X| S(   Nu   pku   invalid_choiceR   (   R   R    R;  R=   RI   R|   RH   RJ   t   DoesNotExistR   RP   (   R[   Rt   t   key(    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR     s    c         C  s   t  j |  |  S(   N(   R   t   validate(   R[   Rt   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyRH    s    c         C  s\   |  j  r t S| d  k	 r | n d } | d  k	 r7 | n d } t |  j |   t |  k S(   Nu    (   t   disabledR6   R    R   R9  (   R[   R   R%   t   initial_valuet
   data_value(    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR     s
    	N(   RD   R^   R  R   R0  R1  R5  RF   R    R]   R<   R=  R@  RB  t   propertyR=   R:  RD  R   t   _set_choicesRA  R9  R   RH  R   (    (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR  }  s&   								
	t   ModelMultipleChoiceFieldc           B  s   e  Z d  Z e Z e Z i e d  d 6e d  d 6e d  d 6Z e	 d d d d d  Z d	   Z d
   Z d   Z d   Z d   Z RS(   u9   A MultipleChoiceField whose choices are a model QuerySet.u   Enter a list of values.u   listuE   Select a valid choice. %(value)s is not one of the available choices.u   invalid_choiceu0   "%(pk)s" is not a valid value for a primary key.u   invalid_pk_valueu    c   	   
   O  s2   t  t |   j | d  | | | | | | |  d  S(   N(   Rb   RN  R]   R    (	   R[   R=   R   R#   R%  R   R<  R.  RW   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR]     s    c         C  s   | s
 g  St  |  j |   S(   N(   R9   t   _check_values(   R[   Rt   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR     s    c         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  n  |  j	 |  } |  j
 |  | S(   Nu   requiredR   u   list(   R9  R   R   RP   R=   R  R   R9   R   RO  t   run_validators(   R[   Rt   R   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR     s    c      	     sA  |  j  p d   y t |  } Wn* t k
 rK t |  j d d d  n Xxi | D]a } y |  j j i |   6  WqS t t f k
 r t |  j d d d d i | d 6 qS XqS W|  j j i | d   6  } t   f d   | D  } xI | D]A } t	 |  | k r t |  j d d d d i | d	 6 q q W| S(
   u   
        Given a list of possible PK values, returns a QuerySet of the
        corresponding objects. Raises a ValidationError if a given value is
        invalid (not a valid PK, not in the queryset, etc.)
        u   pku   listR   u   invalid_pk_valuet   paramsu   %s__inc         3  s$   |  ] } t  t |     Vq d  S(   N(   R   R5   (   R   R   (   RG  (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pys	   <genexpr>+  s    u   invalid_choiceu   value(
   R;  t	   frozensetRH   R   RP   R=   R  R|   Rg   R   (   R[   Rt   R   R   t   pkst   val(    (   RG  s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyRO    s0    c         C  sq   t  | d  r[ t | t j  r[ t  | d  r[ g  | D] } t t |   j |  ^ q9 St t |   j |  S(   Nu   __iter__u   _meta(   R;   R   R   R   Rb   RN  R9  (   R[   Rt   Rw   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR9  5  s
    )c         C  s   |  j  r t S| d  k r" g  } n  | d  k r7 g  } n  t |  t |  k rS t St d   |  j |  D  } t d   | D  } | | k S(   Nc         s  s   |  ] } t  |  Vq d  S(   N(   R   (   R   Rt   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pys	   <genexpr>E  s    c         s  s   |  ] } t  |  Vq d  S(   N(   R   (   R   Rt   (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pys	   <genexpr>F  s    (   RI  R6   R    R   RF   Rg   R9  (   R[   R   R%   t   initial_sett   data_set(    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyR   <  s    			N(   RD   R^   R  R   R#   R   t   hidden_widgetR   R0  RF   R    R]   R   R   RO  R9  R   (    (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyRN    s   				%	c         C  s=   |  d  k	 o< t |  d  o< |  j j d  k	 p< |  j j d  k	 S(   Nu   _meta(   R    R;   R   R   R-   (   t
   form_class(    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyt   modelform_defines_fieldsJ  s    N(@   R  t
   __future__R    t   collectionsR   t	   itertoolsR   t   django.core.exceptionsR   R   R   R   t   django.forms.fieldsR   R   t   django.forms.formsR	   R
   t   django.forms.formsetsR   R   t   django.forms.utilsR   t   django.forms.widgetsR   R   R   t   django.utilsR   t   django.utils.encodingR   t   django.utils.textR   R   t   django.utils.translationR   R   R   t   __all__RE   R    R1   R:   RA   RF   RY   R   RZ   R_   Rd   t   with_metaclassR   R   R   R6   R  R  R*  R,  R   R1  R  RN  RY  (    (    (    s5   /tmp/pip-unpacked-wheel-BAJOf3/django/forms/models.pyt   <module>   sn   "		'			WI"			R 			k7			&)n_