o 3a@s4dZddlZddlmZddlmZmZmZmZddl m Z m Z ddl m Z mZddlmZmZddlmZdd lmZmZmZmZdd lmZdd lmZmZdd lmZm Z!d Z"dZ#d=ddZ$d=ddZ%ddZ&    d>ddddZ'GdddZ(GdddeZ)Gddde Z*Gdd d e*e)d!Z+e+dddddddddf d"d#Z,Gd$d%d%eZ-e+de-d&d'd'ddddd'dddddd'dddfd(d)Z.Gd*d+d+e-Z/d?d,d-Z0e+e/dddd.d'ddddd'dddddd'dddfd/d0Z1Gd1d2d2e Z2Gd3d4d4Z3Gd5d6d6Z4Gd7d8d8e Z5Gd9d:d:e5Z6d;d<Z7dS)@z[ Helper functions for creating Form classes from Django models and database field objects. N)chain)NON_FIELD_ERRORS FieldErrorImproperlyConfiguredValidationError) ChoiceFieldField)BaseFormDeclarativeFieldsMetaclass) BaseFormSetformset_factory) ErrorList) HiddenInputMultipleHiddenInput RadioSelectSelectMultiple)RemovedInDjango40Warning)capfirst get_text_list)gettext gettext_lazy) ModelForm BaseModelForm model_to_dictfields_for_modelModelChoiceFieldModelMultipleChoiceField ALL_FIELDSBaseModelFormSetmodelformset_factoryBaseInlineFormSetinlineformset_factorymodelform_factory__all__c Csddlm}|j}|j}g}|jD]\}|jr!t||js!|j|vr"q|dur,|j|vr,q|r4|j|vr4q| rX||jj j |j |j||jrX||j||jj jvrXqt||jrd||q||||jq|D] }||||jqp|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) django.dbr$_meta cleaned_datafieldseditable isinstance AutoFieldname has_defaultfieldwidgetvalue_omitted_from_datadatafiles add_prefixget empty_values FileFieldappendsave_form_data) forminstancer(excluder$optsr'file_field_listfr?5/usr/lib/python3/dist-packages/django/forms/models.pyconstruct_instance s0   "  rAcCsh|j}i}t|j|j|jD]#}t|ddsq|dur!|j|vr!q|r)|j|vr)q||||j<q|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&rconcrete_fieldsprivate_fields many_to_manygetattrr,value_from_object)r:r(r;r<r1r>r?r?r@rIs  rcCsddlm}m}m}t|drBt|drD|}|rF|}t||s(|di|}|||ddM}|j||jj j ||_dSdSdSdS) z=Apply limit_choices_to to the formfield's queryset if needed.r)ExistsOuterRefQquerysetget_limit_choices_topkrLNr?) django.db.modelsrGrHrIhasattrrKr*rJfiltermodel _base_manager) formfieldrGrHrIlimit_choices_tocomplex_filterr?r?r@#apply_limit_choices_to_to_formfieldbs  rVTapply_limit_choices_toc sig|j} ddlmfdd| jD} tt| j| | jD]} t| ddsF|durE| j |vrEdus;| j vrEt d| j |j fq!|durP| j |vrPq!rX| j vrXq!i}|rh| j |vrh|| j |d <|t kss|rw| j |vrwd |d <|r| j |vr|| j |d <|r| j |vr|| j |d <|r| j |vr|| j |d<| r| j | vr| | j |d<|dur| j di|}nt|std|| fi|}|r| rt||| j <q!| j q!|rfdd|DS)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)rcsg|] }t|r|qSr?)r*.0r>) ModelFieldr?r@ z$fields_for_model..r)FNzH'%s' cannot be specified for %s model form as it is a non-editable fieldr/Tlocalizelabel help_texterror_messages form_classz1formfield_callback must be a function or callablecs,i|]}r |vr|vr||qSr?)r4rY)r; field_dictignoredr?r@ s z$fields_for_model..r?)r&rNrrCsortedrrBrDrEr,r__name__rrScallable TypeErrorrVr7)rQr(r;widgetsformfield_callbacklocalized_fieldslabels help_textsra field_classesrXr<sortable_private_fieldsr>kwargsrSr?)r[r;rcrdr@rrs^"   rc@seZdZdddZdS)ModelFormOptionsNcCst|dd|_t|dd|_t|dd|_t|dd|_t|dd|_t|dd|_t|dd|_t|dd|_t|d d|_ dS) NrQr(r;rjrlrmrnraro) rErQr(r;rjrlrmrnraro)selfoptionsr?r?r@__init__szModelFormOptions.__init__N)rg __module__ __qualname__rur?r?r?r@rrsrrcseZdZfddZZS)ModelFormMetaclassc szd}|D]}t|drt|jdr|jj}nq|d|}t||||}|tfkr.|Stt|dd}|_ dD]} t|| } t | t rX| t krXd|j | | d} t| q;|jr|jdurl|jdurltd||jt krtd|_t|j|j|j|j||j|j|j|j|jdd } d d | D} | |j}|rd }|d ||jj f}t|| |jn|j} | |_ |S) NMetark)r(r;rlzP%(model)s.Meta.%(opt)s cannot be a string. Did you mean to type: ('%(value)s',)?)rQoptvaluez|Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form %s needs updating.FrWcSsh|]\}}|s|qSr?r?)rZkvr?r?r@ sz-ModelFormMetaclass.__new__..z&Unknown field(s) (%s) specified for %sz, )!rOrzrkpopsuper__new__rrrrEr&r*strrrgrirQr(r;rrrjrlrmrnraroitems differencedeclared_fieldsjoinrupdate base_fields)mcsr,basesattrsbase_formfield_callbackbrk new_classr<r{r|msgr(none_model_fieldsmissing_fieldsmessage __class__r?r@rsb        zModelFormMetaclass.__new__)rgrwrxr __classcell__r?r?rr@rysryc steZdZdddddedddddf fdd ZddZdd Zd d Zd d ZddZ ddZ dddZ de _ Z S)rNid_%sFc  s|j} | jdur td| dur| |_i} n | |_t| | j| j} |dur,| |d|_t j ||||| |||| | d |j D]}t |qDdS)Nz'ModelForm has no model class specified.F)use_required_attributerenderer) r&rQ ValueErrorr:rr(r;r_validate_uniquerruvaluesrV)rsr1r2auto_idprefixinitial error_class label_suffixempty_permittedr:rrr< object_datarSrr?r@ru!s&     zBaseModelForm.__init__cCsg}|jjjD][}|j}||jvr||jq|jjr)||jjvr)||jq|jjr:||jjvr:||jq||jvrF||jq|j|}|j|}|j sb|j sb||j vrb||jq|S)z For backwards-compatibility, exclude several types of fields from model validation. See tickets #12507, #12521, #12553. ) r:r&r(r,r7r;_errorsr'r4blankrequiredr5)rsr;r>r. form_field field_valuer?r?r@_get_validation_exclusions>s"    z(BaseModelForm._get_validation_exclusionscCs d|_|jS)NT)rr'rsr?r?r@cleangszBaseModelForm.cleancCs|j}t|dr |j}nt|i}|D]8\}}|tkr*|jr*t|jvr*|jt}n ||jvr6|j|j}nq|D]}t|trK|j |vrK||j |_ q9q| d|dS)N error_dict) r&rOrrrrar(r*rcoder add_error)rserrorsr<rr.messagesrarr?r?r@_update_errorsks$       zBaseModelForm._update_errorsc Cs|j}|}|jD]\}}t|tr||q z t||j|j|j |_Wnt y?}z | |WYd}~nd}~wwz |jj |ddWnt ya}z | |WYd}~nd}~ww|j rk|dSdS)NF)r;validate_unique)r&rr(rr*InlineForeignKeyFieldr7rAr:r;rr full_cleanrr)rsr<r;r,r.er?r?r@ _post_cleans*   zBaseModelForm._post_cleanc CsN|}z |jj|dWdSty&}z ||WYd}~dSd}~ww)z Call the instance's validate_unique() method and update the form's validation errors if any were raised. r;N)rr:rrr)rsr;rr?r?r@rszBaseModelForm.validate_uniquecCs|j}|jj}|jj}|jj}t|j|jD]'}t|dsq|r&|j |vr&q|r.|j |vr.q|j |vr=| |j||j qdS)zS Save the many-to-many fields and generic relations for this form. r8N) r'r&r;r(r:rrDrCrOr,r8)rsr'r;r(r<r>r?r?r@ _save_m2ms  zBaseModelForm._save_m2mTcCsZ|jrtd|jjj|jjjrdfdf|r&|j||jS|j|_ |jS)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.createdchanged) rrr:r& object_name_stateaddingsaversave_m2m)rscommitr?r?r@rs   zBaseModelForm.saveT)rgrwrxr rurrrrrrr alters_datarr?r?rr@r s) rc@s eZdZdS)rN)rgrwrxr?r?r?r@rsr) metaclassc Csd|i} |dur || d<|dur|| d<|dur|| d<|dur$|| d<|dur,|| d<|dur4|| d<| dur<| | d <| durD| | d <t|d rM|jfnd } td | | } |r\t|| _|jd }| |d}t| dddurzt| dddurztdt|||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. rQNr(r;rjrlrmrnrarorzr?Form)rzrkzZCalling modelform_factory without defining 'fields' or 'exclude' explicitly is prohibited.)rOrztype staticmethodrkrgrEr)rQr9r(r;rkrjrlrmrnrarorrrz class_nameform_class_attrsr?r?r@r"s>'   r"cseZdZdZdZeZ  d*ddfdd ZfddZd d Z d d Z fd dZ ddZ d+ddZ d+ddZd+ddZd+ddZde_ddZddZddZd d!Zd"d#Zd+d$d%Zd+d&d'Zfd(d)ZZS),rzO A ``FormSet`` for editing a queryset and/or adding new objects to it. Nr)rc s0||_||_tjdi||||d|dS)N)r1r2rrr?)rJ initial_extrarru)rsr1r2rrrJrrqrr?r@ruAs$zBaseModelFormSet.__init__cs|js t|StS)z=Return the number of forms that are required in this FormSet.)is_boundlen get_querysetrinitial_form_countrrr?r@rGs  z#BaseModelFormSet.initial_form_countcCs*t|dsdd|D|_|j|S)N _object_dictcSsi|]}|j|qSr?rMrZor?r?r@reOz5BaseModelFormSet._existing_object..)rOrrr4)rsrLr?r?r@_existing_objectMs  z!BaseModelFormSet._existing_objectcCs$|jdur|j}|jdus|jS)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_fieldget_related_field to_pythonrsr.r?r?r@_get_to_pythonRs   zBaseModelFormSet._get_to_pythonc s||k}|rR|jrId|||jjjjf}z|j|}Wn ty(YnDw| |jjj}z||}Wn t y@Yn,w| ||d<n#| ||d<n|j rlz |j |||d<Wn tykYnwtj|fi|}|rd|j|jjjj_|S)Nz%s-%sr:rT)rrr3rQr&rLr,r1KeyErrorrrrrr IndexErrorr_construct_formr(r)rsirq pk_requiredpk_keyrLrr9rr?r@r[s6     z BaseModelFormSet._construct_formcCsLt|ds#|jdur|j}n|jj}|js ||jjjj }||_ |j S)N _queryset) rOrJrQ_default_managerrorderedorder_byr&rLr,r)rsqsr?r?r@r}s   zBaseModelFormSet.get_querysetTcCs |j|dS)z8Save and return a new model instance for the given form.rrrsr9rr?r?r@save_new zBaseModelFormSet.save_newcCs |j|dS)z>Save and return an existing model instance for the given form.rr)rsr9r:rr?r?r@ save_existingrzBaseModelFormSet.save_existingcCs|r|dSdS)z#Deletes an existing model instance.N)delete)rsobjrr?r?r@delete_existings z BaseModelFormSet.delete_existingcs0|sg_fdd}|_||S)z Save model instances for every form, adding and changing instances as necessary, and return the list of instances. csjD]}|qdSrv) saved_formsr)r9rr?r@rs  z'BaseModelFormSet.save..save_m2m)rrsave_existing_objectssave_new_objects)rsrrr?rr@rs  zBaseModelFormSet.savecCs |dSrv)rrr?r?r@r zBaseModelFormSet.cleancst}t}jfddjD}|D]}jj|d\}}||||qg}|D]O\}} t} |D]Efdd| D} tdd| D} | rd| vr| | vr|| |   gj t <| D] } | jvr{j| =qp| | q.rc3s2|]}|jvr|jvr|nj|VqdSrv)r' unique_fieldsrZr.)r9rsr?r@ s  z3BaseModelFormSet.validate_unique..css8|]}t|dr |n t|trt|n|VqdS) _get_pk_valN)rOrr*listtuple)rZdr?r?r@rs date)set deleted_formsformsrr:_get_unique_checksrrr7get_unique_error_messagerget_form_errorrrr'addyearmonthdayrEget_date_error_messager)rsall_unique_checksall_date_checks valid_formsr; unique_checks date_checksruclass unique_check seen_datarow_datar. date_checklookup unique_forr date_datar1r?)r9rrsr@rsf          z BaseModelFormSet.validate_uniquecCs:t|dkrtdd|diStddt|tdiS)Nz0Please correct the duplicate data for %(field)s.r.rzFPlease correct the duplicate data for %(field)s, which must be unique.and)rrr_rsr r?r?r@rs z)BaseModelFormSet.get_unique_error_messagecCs$td|d|dt|ddS)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)rr)rsrr?r?r@rs z'BaseModelFormSet.get_date_error_messagecCstdS)Nz*Please correct the duplicate values below.)rrr?r?r@r szBaseModelFormSet.get_form_errorcCsg|_g|_|js gSg}|j}|jD]=}|j}|jdurq||vr0|j||j||dq|rP|j||j f||j |||d|sP|j |q|SNr) changed_objectsdeleted_objects initial_formsrr:rLr7r has_changed changed_datarr)rsrsaved_instancesrr9rr?r?r@rs(    z&BaseModelFormSet.save_existing_objectscCsZg|_|jD]#}|s q|jr||rq|j|j||d|s)|j|q|jSr) new_objects extra_formsr can_delete_should_delete_formr7rr)rsrr9r?r?r@r*s  z!BaseModelFormSet.save_new_objectsc s$ddlmm}m}|jjj|_}fdd|s#|j|j vr|j r2|j j j r-dn|j j}nz|dur?||j}nd}Wn tyMd}Ynwt|||fr]|jjj}n|jj}||j j j}|jjrz|jj|jjt}nt}t||d|d|j |jj<t||dS)z0Add a hidden field for the object's primary key.r)r+ ForeignKey OneToOneFieldcs6|j p|jpt|p|jo|jjo|jjjjSrv)r) auto_createdr*r parent_linkrQr&rLrMr+pk_is_not_editabler?r@r*Bsz7BaseModelFormSet.add_fields..pk_is_not_editableNF)rrr/)rNr+r%r&rQr&rL _pk_fieldr,r(rr:rrrrr*rrusingdbrjr4rrr add_fields) rsr9indexr%r&rLpk_valuerr/rr)r@r.8s.  zBaseModelFormSet.add_fields)NNrNNr)rgrwrx__doc__rQrrrurrrrrrrrrrrrrrrrrr.rr?r?rr@r8s6  "    I   rrFc Cszt|dd}t|d|durt|d| durtdt|||| || | | |||d }t||||||||| ||d }||_|S)z8Return a FormSet class for the given Django model class.rzNr(r;z]Calling modelformset_factory without defining 'fields' or 'exclude' explicitly is prohibited.) r9r(r;rkrjrlrmrnraro) extramin_nummax_num can_orderr# validate_min validate_max absolute_maxcan_delete_extra)rErr"r rQ)rQr9rkformsetr2r#r5r4r(r;rjr7rlrmrnrar3r6ror8r9metaFormSetr?r?r@rds&   rcspeZdZdZ  dfdd ZfddZfdd Zed d Zdfd d Z fddZ fddZ Z S)r z0A formset for child objects related to a parent.NFc  s|dur |jj|_n||_||_|dur|jj}|jjdur.|jdi|jj|ji}n| }|jjh|_ t j ||f||d||j jjrr|jj|j jjvrtt|j jjtrft|j jj|j j_|j jj|jjdSdSdS)N)rrJr?)fkrrQr: save_as_newrrLrPr,nonerrrur9r&r(r*rrr7) rsr1r2r:r>rrJrqrrr?r@rus   zBaseInlineFormSet.__init__cs|jrdStS)Nr)r>rrrrr?r@rs z$BaseInlineFormSet.initial_form_countc stj|fi|}|jr8t|jdd}|durd|j_d|j||jj<d|j||j j<|dur8||j_|j j }|j j j |j j jjj jkrXt|j |j j j }t|d|}t|j |j ||S)N_mutableTrL)rrr>rEr1r@r3r+r,r=r:rLrrrQr&setattr get_attname)rsrrqr9mutablefk_valuerr?r@rs z!BaseInlineFormSet._construct_formcCs|jjj|jdddS)N)rQ+)r=rget_accessor_namerQreplace)clsr?r?r@get_default_prefixsz$BaseInlineFormSet.get_default_prefixTcs$t|j|jj|jtj||dSr)rAr:r=r,rrrrr?r@rszBaseInlineFormSet.save_newcst|||j|jkr|jj}ddi}n|jj}dt|j|dt|jj i}|jj j |jj j j jjkr>|jj j |d<|jjjre|ddurT|jj |d}n|jj j}|ret|j|jdt|jfi||j|<dS)Npk_fieldTr_to_field)rr.r+r=r,rEr(r4r verbose_namerrrQr&rLr:rr get_fieldr-rAattnamer)rsr9r/r,rqrLrr?r@r.s     zBaseInlineFormSet.add_fieldscsfdd|D}t|S)Ncsg|] }|jjkr|qSr?)r=r,rrr?r@r\sz>BaseInlineFormSet.get_unique_error_message..)rrrrrr@rs z*BaseInlineFormSet.get_unique_error_message)NNNFNNr) rgrwrxr1rurr classmethodrJrr.rrr?r?rr@r s    r csddlm|j}rMfdd|jD}t|dkr?|d}t|r3|jjkr=|jjjvr=t djj f|S|sKt d|jj f|Sfdd|jD}t|dkrd|d}|S|sv|rjd St d |jj jj ft d |jj jj f) 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%csg|] }|jkr|qSr?)r,rYfk_namer?r@r\r]z$_get_foreign_key..rz)fk_name '%s' is not a ForeignKey to '%s'.z'%s' has no field named '%s'.cs8g|]}t|r|jjks|jjjvr|qSr?)r*rrQr&get_parent_listrY)r% parent_modelr?r@r\s Nz'%s' has no ForeignKey to '%s'.zR'%s' has more than one ForeignKey to '%s'. You must specify a 'fk_name' attribute.) rNr%r&r(rr*rrQrSrr_)rTrQrRcan_failr< fks_to_parentr=r?)r%rRrTr@_get_foreign_keysP    #  rWrcCst|||d}|jr d} id|d| d|d|d| d|d |d |d |d | d | d|d| d|d|d|d||||d}t|fi|}||_|S)z Return an ``InlineFormSet`` for the given kwargs. ``fk_name`` must be provided if ``model`` has more than one ``ForeignKey`` to ``parent_model``. rQrr9rkr:r2r#r5r(r;r3r4rjr6r7rlrmrnra)ror8r9)rWuniquerr=)rTrQr9r:rRr(r;r2r5r#r4rkrjr7rlrmrnrar3r6ror8r9r=rqr<r?r?r@r!sZ     r!csHeZdZdZeZdediZdddfdd Zd d Z d d Z Z S) rzw 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)rKrLcs`||_||_||_|jdur!|jrt|j|j|d<n|jj|d<d|d<tj|i|dS)NrFr)parent_instancerKrLrErLrru)rsrZrKrLargsrqrr?r@ruXs  zInlineForeignKeyField.__init__cCs`||jvr |jr dS|jS|jrt|j|j}n|jj}t|t|kr-t|jddd|jS)NrYr) r5rKrZrLrErLrrra)rsr|origr?r?r@rds zInlineForeignKeyField.cleancCsdSNFr?)rsrr1r?r?r@rssz!InlineForeignKeyField.has_changed) rgrwrxr1rr/rdefault_error_messagesrurrrr?r?rr@rNs rc@s$eZdZddZddZddZdS)ModelChoiceIteratorValuecCs||_||_dSrv)r|r:)rsr|r:r?r?r@ruxs z!ModelChoiceIteratorValue.__init__cCs t|jSrv)rr|rr?r?r@__str__|s z ModelChoiceIteratorValue.__str__cCst|tr|j}|j|kSrv)r*r`r|)rsotherr?r?r@__eq__s  zModelChoiceIteratorValue.__eq__N)rgrwrxrurarcr?r?r?r@r`ws r`c@s4eZdZddZddZddZddZd d Zd S) ModelChoiceIteratorcCs||_|j|_dSrv)r.rJrr?r?r@rus zModelChoiceIterator.__init__ccsJ|jjdurd|jjfV|j}|js|}|D]}||VqdS)NrF)r. empty_labelrJ_prefetch_related_lookupsiteratorchoice)rsrJrr?r?r@__iter__s zModelChoiceIterator.__iter__cCs |j|jjdur dSdS)Nrr)rJcountr.rerr?r?r@__len__s zModelChoiceIterator.__len__cCs|jjdup |jSrv)r.rerJexistsrr?r?r@__bool__szModelChoiceIterator.__bool__cCst|j|||j|fSrv)r`r. prepare_valuelabel_from_instancersrr?r?r@rhs zModelChoiceIterator.choiceN)rgrwrxrurirkrmrhr?r?r?r@rds   rdc seZdZdZdediZeZdddddddddd d d Zd d Z fddZ ddZ ddZ e e e ZddZddZe eejZfddZddZddZddZZS) rz1A ChoiceField whose choices are a model QuerySet.rYzGSelect a valid choice. That choice is not one of the available choices.z ---------TNrFF) rerr/r_rr` to_field_namerTrc Ks^tj|f|||||d| |r|dust|jtr!| s!d|_n||_||_| |_||_dS)N)rr/r_rr`) rrur*r/rrerJrTrq) rsrJrerr/r_rr`rqrTrrqr?r?r@rus(  zModelChoiceField.__init__cCst|jr |S|jS)z Return ``limit_choices_to`` for this form field. If it is a callable, invoke it and return the result. )rhrTrr?r?r@rKs z%ModelChoiceField.get_limit_choices_tocs*tt||}|jdur|j|_|Srv)rr __deepcopy__rJall)rsmemoresultrr?r@rrs  zModelChoiceField.__deepcopy__cCs|jSrv)rrr?r?r@ _get_querysetszModelChoiceField._get_querysetcCs$|durdn||_|j|j_dSrv)rsrchoicesr/)rsrJr?r?r@ _set_querysetszModelChoiceField._set_querysetcCst|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. rrpr?r?r@rosz$ModelChoiceField.label_from_instancecCst|dr|jS||S)N_choices)rOrzrgrr?r?r@ _get_choicess  zModelChoiceField._get_choicescs.t|dr|jr||jS|jSt|SNr&)rOrqserializable_valuerLrrnrsr|rr?r@rns   zModelChoiceField.prepare_valuec Csz||jvrdSz|jp d}t||jjrt||}|jjdi||i}W|Stt|jjj fy<t |j dddw)NrLrYr\r?) r5rqr*rJrQrEr4rri DoesNotExistrra)rsr|keyr?r?r@rs   zModelChoiceField.to_pythoncCs t||Srv)rvalidater~r?r?r@r rzModelChoiceField.validatecCs@|jrdS|dur |nd}|dur|nd}t||t|kS)NFrF)disabledrrn)rsrr1 initial_value data_valuer?r?r@rs zModelChoiceField.has_changed)rgrwrxr1rr_rdrgrurKrrrvrxpropertyrJror{r _set_choicesrwrnrrrrr?r?rr@rs,      rcsleZdZdZeZeZedededdZ fddZ dd Z d d Z d d Z fddZddZZS)rz9A MultipleChoiceField whose choices are a model QuerySet.zEnter 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_listrYinvalid_pk_valuec sPtj|fddi||jddur&tjdtdd|jd|jd<dSdS)NrerzFThe 'list' error message key is deprecated in favor of 'invalid_list'.r) stacklevelr)rrurar4warningswarnr)rsrJrqrr?r@ru"sz!ModelMultipleChoiceField.__init__cCs|sgSt||Srv)r _check_valuesr~r?r?r@r,sz"ModelMultipleChoiceField.to_pythoncCsr||}|jr|st|jddd|js|s|jSt|ttfs-t|jddd| |}| ||S)Nrr\r) rnrrrarJr?r*rrrrun_validators)rsr|rr?r?r@r1s      zModelMultipleChoiceField.cleanc s|jpdzt|}Wntyt|jdddw|D]$}z |jjd i|iWqttfyBt|jddd|idw|jjd id|i}fdd|D}|D]}t||vrot|jd d d |idq[|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.) rLrr\r)rparamsz%s__incsh|] }tt|qSr?)rrErrr?r@r]r]z9ModelMultipleChoiceField._check_values..rYr|Nr?) rq frozensetrirrarJrPrr)rsr|rLrpksvalr?rr@rBs<    z&ModelMultipleChoiceField._check_valuescsDt|drt|tst|dstjfdd|DSt|S)Nrir&csg|]}|qSr?r?)rZr~rnr?r@r\lrz:ModelMultipleChoiceField.prepare_value..)rOr*rrrnr~rrr@rngs  z&ModelMultipleChoiceField.prepare_valuecCs`|jrdS|dur g}|durg}t|t|krdSdd||D}dd|D}||kS)NFTcSh|]}t|qSr?ryrZr|r?r?r@rxrz7ModelMultipleChoiceField.has_changed..cSrr?ryrr?r?r@ryr)rrrn)rsrr1 initial_setdata_setr?r?r@rosz$ModelMultipleChoiceField.has_changed)rgrwrxr1rr/r hidden_widgetrr_rurrrrnrrr?r?rr@rs   %rcCs"t|do|jjdup|jjduSr|)rOr&r(r;)rbr?r?r@modelform_defines_fields}s   r)NN) NNNNNNNNNr^)8r1r itertoolsrdjango.core.exceptionsrrrrdjango.forms.fieldsrrdjango.forms.formsr r django.forms.formsetsr r django.forms.utilsr django.forms.widgetsrrrrdjango.utils.deprecationrdjango.utils.textrrdjango.utils.translationrrrr#rrArrVrrrryrrr"rrr rWr!rr`rdrrrr?r?r?r@s|     ) Y H@ U.  d8 /)s f