Django -- where is the many to many picker widget?
Asked Answered
B

2

6

For each row of table A, I want to be able to add one or more rows from table B.

The Django admin has an example, the user permissions picker -- I attach a screen shot.

Django picker for user permissions, a many to many picker

Each user can have any number of permissions. The permissions start on the left. When you add one, it moves to the right. You can move them back and forth.

That is what I want for two of my own models. How do I get this widget?

I first thought I needed a many to one widget, but on further thought, I think this is many to many. After a user gets a permission, that permission is still available to other users. A user can have several permissions; for a particular permission, the same permission can be given to several users -- must be many to many. My two tables work the same way.

I need the widget on add and update record pages accessible to users; I do not need it on the admin pages. (I was lucky to find a perfect example of what I need on the user admin page.)

Breakout answered 31/12, 2017 at 5:41 Comment(0)
C
10

In your admin.py

class your_model_admin(admin.ModelAdmin):
    ...
    filter_horizontal = ('field_name',) 

This will make a widget same as permission field for the field_name field too

Counterscarp answered 31/12, 2017 at 5:46 Comment(0)
B
2

I asked by putting a ticket into code.djangoproject.com.

The answer:

The modules are in there, and you can use them.

you can use the widget from

from django.forms.widgets import SelectMultiple

The filter_horizontal / filter_vertical widget is accessible from

from django.contrib.admin.widgets import FilteredSelectMultiple

https://code.djangoproject.com/ticket/28993#comment:1

However, the modules are not offered up for use on forms because they rely on javascript and backwards compatibility cannot be guaranteed.

https://code.djangoproject.com/ticket/28993#comment:2

???

Breakout answered 15/1, 2018 at 17:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.