开发者

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 t开发者_运维问答he 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


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


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 %}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜