Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
176 views
in Technique[技术] by (71.8m points)

django - Why in some case forms.hiddenInput() with initial value return None and some time the initial value?

I have 2 forms (form 1 RandomizationForm = modelForm and form 2 RallocationForm= form) with the same hidden field with initial value

but they do not have the same behavior:

  • form 1 return the initial value in self.cleaned_data['ran_log_sit']
  • form 2 return None in self.cleaned_data['ran_log_sit']

So I have to had clean_ran_log_sit(self) method to set initial value when self.cleaned_data['ran_log_sit'] return None

I don't know why and so I am not sure with my code

form 1:

class RandomisationForm(forms.ModelForm):
    def __init__(self, request, *args, **kwargs):
        super(RandomisationForm, self).__init__(*args, **kwargs)
...
        SITE_CONCERNE = Site.options_list_site_concerne_randomization_or_reallocation(self.user,self.user_pays,self.user_site_type,self.language)
        if self.user_site_type == 'International':
            self.fields["ran_log_sit"] = forms.ChoiceField(label = _("Randomization site"), required=True, widget=forms.Select, choices=SITE_CONCERNE)
        elif self.user_site_type == 'National':
            self.fields["ran_log_sit"] = forms.ChoiceField(label = _("Randomization site"), widget=forms.Select, choices=SITE_CONCERNE)
        else:
            self.fields["ran_log_sit"] = forms.ChoiceField(label = _("Randomization site"), widget=forms.HiddenInput(), choices=SITE_CONCERNE, initial = self.user_site, disabled=True)
            
 ...


    # vérification que la randomisation est possible (i.e. au moins une boite d'aspirine et une boite de placebo)
    def clean(self):
        cleaned_data = super(RandomisationForm, self).clean()

        # validation anti-doublon
        if Randomisation.objects.filter(ran_num = self.data.get('ran_num').upper()).exists():
            bras = Randomisation.objects.get(ran_num = self.data.get('ran_num').upper()).ran_bra
            bras_libelle = OptionDeThesaurus.objects.get(the_id=10,the_opt_cod=bras).the_opt_lab_eng
            med = current_drug(self.data.get('ran_num').upper())
            raise forms.ValidationError(str(_('This patient has already been randomized in arm '))+str(_(bras_libelle))+str(_(' - current box number '))+str(med)+str(_('. Please control patient number.')))

        # vérification stock
        print("cleaned_data['ran_log_sit']",cleaned_data['ran_log_sit'])
        pays = Site.objects.get(sit_abr = cleaned_data['ran_log_sit']).reg.pay.pay_abr
        if patient_code_is_valid(cleaned_data['ran_num']) and not is_randomizable(pays,cleaned_data['ran_log_sit']):
            raise ValidationError(_('Insufficient drug stock in the selected site to randomize this patient'))

        # remplissage conditionnel: si randomisation par téléphone alors on attends le nom de la personne contactée par téléphone
        if cleaned_data['ran_pro'] == '2' and cleaned_data['ran_pro_per'] == '':
            self.add_error('ran_pro_per', 'Please precise name of the person reached by phone')

    class Meta:
        model = Randomisation
        # Tous les champs sauf les champs de log et les champs TB treatment et Drug batch number
        fields = ('ran_log_sit','ran_num','ran_dat','ran_inv','ran_pro','ran_pro_per','ran_crf_inc','ran_tbc','ran_crf_eli','ran_cri','ran_sta','ran_vih',)

    def clean_ran_log_sit(self):
        data = self.cleaned_data['ran_log_sit']
        print('data',data)
        pays = Site.objects.get(sit_abr = data).reg.pay.pay_abr
        if not is_randomizable(pays,data):
            raise forms.ValidationError(_('Insufficient drug stock in the selected site to randomize this patient'))
        return data

form 2

class ReallocationForm(forms.Form):

    def __init__(self, request, *args, **kwargs):
        super(ReallocationForm, self).__init__(*args, **kwargs)
...
        if self.user_site_type == 'International':    
            self.fields["ran_log_sit"] = forms.ChoiceField(label = _("Randomization site"), required=True, widget=forms.Select, choices=SITE_CONCERNE) 
        elif self.user_site_type == 'National':
            self.fields["ran_log_sit"] = forms.ChoiceField(label = _("Randomization site"), widget=forms.Select, choices=SITE_CONCERNE)
        else:
            self.fields["ran_log_sit"] = forms.ChoiceField(label = _("Randomization site"), widget=forms.HiddenInput(), choices=SITE_CONCERNE, initial = self.user_site, disabled=True)
        self.fields["patient"] = forms.ChoiceField(label = _("Patient code number :"), widget=forms.Select, choices=PATIENTS)

    def clean_ran_log_sit(self):
        data = self.cleaned_data['ran_log_sit']
        if data == None and self.user_site_type == 'Site':
            data = self.user_site
        return data


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...