3

I have a BooleanField (ran_bug) in my Randomisation models that is displayed as a checkbox. Click on the checkbox sould show 2 other fields that are not mandatory (ran_dem_nom and ran_dem_dat).

My problem is that, when I 'check' the checkbox, it return 'on' instead of true.

And I got an error when I try to registered data:

django.core.exceptions.ValidationError: ["'on' value must be either True, False, or None."]

models.py

class Randomisation(models.Model): ran_ide = models.AutoField(primary_key=True) pay_ide = models.ForeignKey(Pays, on_delete = models.CASCADE) # related country ran_str_num = models.CharField("Logical numerotation", max_length=2, null=True, blank=True) ran_bra = models.CharField("Arm", max_length=1, null=True, blank=True) bra_lib = models.CharField("Arm label", max_length=50, null=True, blank=True) ran_act = models.IntegerField("Activated line", null=True, blank=True) pat = models.CharField("Patient number", max_length=12, unique=True, null=True, blank=True) ran_nai = models.IntegerField("Patient birthdate (year)", blank=True) ran_sex = models.IntegerField("Sex", null=True, blank=True) ran_st1 = models.IntegerField("Stratification variable 1", blank=True) ran_st2 = models.IntegerField("Stratification variable 2", blank=True) ran_bug = models.BooleanField("Use of alternative randomization procedure?", null=True, blank=True) ran_dem_nom = models.CharField("Name of the person asking for randomization", max_length=12, null=True, blank=True) # hide at pageload ran_dem_dat = models.DateField("Date of demand", null=True, blank=True) # hide at pageload ran_log = models.CharField("User", max_length=12, null=True, blank=True) ran_dat = models.DateTimeField("Date", null=True, auto_now_add=True, blank=True) 

forms.py

class RandomizationEditForm(forms.Form): def __init__(self, request, *args, **kwargs): super(RandomizationEditForm, self).__init__(*args, **kwargs) self.user_country = Pays.objects.get(pay_ide = request.session.get('user_country')) self.user_site_type = request.session.get('user_site_type') PAYS = Pays.options_list(self.user_country,self.user_site_type,'fr') SEXE = Thesaurus.options_list(2,'fr') STRATE_1 = Thesaurus.options_list(3,'fr') STRATE_2 = Thesaurus.options_list(4,'fr') YES = [(None,''),(0,'Non'),(1,'Oui'),] self.fields["pay_ide"] = forms.IntegerField(label = "Pays", initial=2, widget=forms.HiddenInput()) self.fields["pat"] = forms.CharField(label = "Numéro patient (XXX-0000)") self.fields['pat'].widget.attrs.update({ 'autocomplete': 'off' }) self.fields["ran_nai"] = forms.IntegerField(label = "Date de naissance (année)", widget=forms.TextInput) self.fields['ran_nai'].widget.attrs.update({ 'autocomplete': 'off' }) self.fields["ran_sex"] = forms.ChoiceField(label = "Sexe", widget=forms.Select, choices=SEXE) self.fields["ran_st1"] = forms.ChoiceField(label = "Gravité de la maladie COVID-19", widget=forms.Select, choices=STRATE_1) self.fields["ran_bug"] = forms.BooleanField(label = "Recours à la procédure de secours ?", required = False) self.fields["ran_dem_nom"] = forms.CharField(label = "Nom de la personne qui demande la randomisation", required = False) self.fields['ran_dem_nom'].widget.attrs.update({ 'autocomplete': 'off' }) self.fields["ran_dem_dat"] = forms.DateField( # input_formats=settings.DATE_INPUT_FORMATS, label = "Date de la demande", initial = timezone.now(), required = False, ) self.fields['ran_dem_dat'].widget.attrs.update({ 'autocomplete': 'off' }) 

JS

$(function(){ $("#div_id_ran_dem_nom").hide(); $("#div_id_ran_dem_dat").hide(); }); // affichage des champs en fonction de la valeur sélectionnée dans la liste $("#div_id_ran_bug").on("change", function(event){ console.log($("#id_ran_bug").val()) if ($("#id_ran_bug").is(":checked")){ $("#div_id_ran_dem_nom").show(); $("#div_id_ran_dem_dat").show(); } else { $("#div_id_ran_dem_nom").hide(); $("#div_id_ran_dem_dat").hide(); } }); 

views.py

def randomization_edit(request): if request.method == "POST": form = RandomizationEditForm(request, data=request.POST or None) if form.is_valid(): # Récupération des données permettant la randomisation randomisation = Randomisation.objects.filter(Q(pay_ide=form.data.get('pay_ide')) & Q(ran_act=1) & Q(ran_st1=form.data.get('ran_st1')) & Q(pat=None)).first() randomisation.pat = form.data.get('pat') randomisation.ran_nai = form.data.get('ran_nai') randomisation.ran_sex = form.data.get('ran_sex') randomisation.ran_bug = form.data.get('ran_bug') randomisation.ran_dem_nom = form.data.get('ran_dem_nom') randomisation.ran_dem_dat = form.data.get('ran_dem_dat') print('ran_bug',form.data.get('ran_bug')) randomisation.ran_log = request.user.username randomisation.ran_dat = timezone.now() randomisation.save() return redirect('randomization:confirmation', pk = randomisation.pk) else: form = RandomizationEditForm(request) return render(request, 'randomization/edit.html', {'form': form}) 
3
  • stackoverflow.com/questions/23911602/… Commented May 25, 2020 at 13:50
  • thanks. But the value return by form.data.get('ran_bug') is always 'on' instead of 'true' or 'false' Commented May 25, 2020 at 13:52
  • Can you post the code that's throwing the exception? A view or something? Perhaps the full exception stack trace? Commented May 25, 2020 at 13:54

1 Answer 1

3

OK, i resolve my problem: form.cleaned_data['ran_bug'] instead of form.data.get('ran_bug')

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this answer cleared it up for me. Finding it, however, was not that easy. I used google: django form form get boolean "=on" true site:stackoverflow.com

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.