0

I've just updated my one field of a model from IntergerField to BooleanField in order to have Checkbox in my admin form but it doesn't...

Instead, It display a select list with 3 option (Unknown, Yes, No)

As mentionned in the Django admin documentation, CheckBoxInput is the default widget so it shloudl works

I try to 'force' CheckBoxInput defining a widget forms.CheckBoxInput but it doesn't works neither...

I need this field to be editable. i verifiy that this not this option that prevent default widget but it isn't.

models.py

class Profile(SafeDeleteModel):
    _safedelete_policy = SOFT_DELETE_CASCADE
    pro_ide = models.AutoField(primary_key = True)
    user = models.OneToOneField(User, on_delete = models.CASCADE)
    site = models.ForeignKey(Site, on_delete = models.CASCADE)
    ...
    pro_mai = models.BooleanField("Send user login/password? (y/n)",default=0, null=True, blank=True)
    ...

admin.py

class ProfileFormAdmin(forms.ModelForm):

    FONCTIONS = Thesaurus.options_list(5,'fr')
    pro_mai = forms.BooleanField(label="Envoyer le mot de passe ? (Oui/Non)",required=False)
    pro_fon = forms.ChoiceField(label="Fonction", widget = forms.Select, choices = FONCTIONS) 

def send_login_password(modeladmin, request, queryset):
    queryset.update(
        pro_mai = 1,
    )
send_login_password.short_description = 'Envoyer login et mot de passe à l\'utilisateur'

class ProfileAdmin(SimpleHistoryAdmin):

    list_display = ('name','contact','pro_mai','fonction',)
    list_editable = ('pro_mai',)
    exclude = ('pro_con','pro_lis_ran', 'pro_lis_rea', 'pro_lis_unb','pro_tel')
    search_fields = ['name']
    actions = [send_login_password]
    form = ProfileFormAdmin
1
  • problem come from sqlite field format that was NULL and edited as NullBoleanField instaed as BooleanField... resolved adding Not null constraint in models Commented Jun 17, 2020 at 17:45

1 Answer 1

1

In your boolean field here:

pro_mai = models.BooleanField("Send user login/password? (y/n)",default=0, null=True, blank=True)

Remove the null value. When you make null=True in your boolean field, it will bring up the select widget instead of the checkbox widget.

You can find more information about it on Django's BooleanField Model field reference.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.