Designer023
Designer023

Reputation: 2002

Django admin checkbox with multiple select

I have a Django app. Pretty basic one at that.

In the model I have a class for items and a class for groups. The groups has a many to many for the items:

items = models.ManyToManyField(item, verbose_name="list of items", max_length=100000, blank=True)

When I add this to the admin section I would like to have a checkbox with multiple select. Is this possible. All of the solutions I have looked at don't sow it in the context of an admin page too. The Django admin page is all I need it to work on as I am not making any custom, public facing pages.

What is the easiest and most simple solution to replace the multiple select box with a multiple checkbox.

PS. I am relatively in-experienced with Django so I need to see what I need to import in the model and admin.

Thanks

Upvotes: 1

Views: 3666

Answers (2)

Valentin Kantor
Valentin Kantor

Reputation: 1844

way based on overriding admin templates

/myproject/templates/admin/myapp/mymodel/change_form.html

{% extends "admin/change_form.html" %}
{{ block.super }} 
<script type='text/javascript' src='/media/js/jquery.js'></script>
<script>
    $(document).ready(function(){
        myselect = $("#id_M2M_FIELD_NAME");

        // here you manipulating with your multiple select,
        // and convert it to checkboxes, or something else.   

    })
</script>
{% endblock %}

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599490

If you know how to do it for a standard modelform, you know how to do it in an admin page too, as they are based on normal forms.

Just define the form as normal, then tell the admin to use it for your model:

class MyModelAdmin(admin.ModelAdmin):
    form = MyFormWithTheMultipleSelect

Upvotes: 3

Related Questions