Tweak admin widget - FilteredSelectMultiple

80 views
Skip to first unread message

Mike Dewhirst

unread,
Jan 4, 2020, 4:01:09 AM1/4/20
to Django users
In the Admin where user group membership is shown and groups can be
selected from choices derived from auth_groups, I want to control the
available choices.

It seems the place to do so might be in admin/widgets.py in the
AutocompleteMixin.optgroups() method a few lines in where it says ...

        choices = (
            (obj.pk, self.choices.field.label_from_instance(obj))
            for obj in
self.choices.queryset.using(self.db).filter(pk__in=selected_choices)
        )

Could a very kind person explain to me how I can hijack this and remove
a couple of groups from the resulting choices?

My use case is that I want members of the 'admin' group to be able to
assign any group membershipto any other userexcept for a couple of
groups which are too permissive. The excepted groups can only be
assigned by a Superuser.

Many thanks for any hints

Cheers

Mike


Integr@te System

unread,
Jan 4, 2020, 10:04:56 AM1/4/20
to django...@googlegroups.com
Hi Mike,

As we look at selected_choice result from iterating v over value (line 428), option_value base on choice(line 438).
so we can limit input parameter of value variable from this def().

Hope if heplful.



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/52923a2a-cc6c-e335-f5fe-6ec6ab004013%40dewhirst.com.au.

Mike Dewhirst

unread,
Jan 5, 2020, 12:43:43 AM1/5/20
to django...@googlegroups.com
On 4/01/2020 9:03 pm, Integr@te System wrote:
> Hi Mike,
>
> As we look at selected_choice result from iterating v over value (line
> 428), option_value base on choice(line 438).
> so we can limit input parameter of value variable from this def().

selected_choices appear in the selected box on screen and choices appear
in the available choices box.

I can see how to tweak either of those if the method was my own. But it
is an Admin method which interfaces with javascript to provide the user
interface for making those selections.

I think my original question was too brief.

I'm thinking now I should be using my own version of AutocompleteMixin
somehow and use formfield_for_manytomany when db_field == "groups" in my
admin.py to get access to it.

formfield_for_manytomany calls the AutocompleteSelectMultiple class
which inherits AutocompleteMixin which has the optgroups() method which
contains selected_choices and choices which are of interest to me.

That said, my question really is "Huh! How do I do that?"

Maybe I'll do some experiments

Thanks in advance if you can short-circuit my research

Cheers

Mike


>
> Hope if heplful.
>
>
>
> On Sat, Jan 4, 2020, 11:00 Mike Dewhirst <mi...@dewhirst.com.au
> <mailto:mi...@dewhirst.com.au>> wrote:
>
> In the Admin where user group membership is shown and groups can be
> selected from choices derived from auth_groups, I want to control the
> available choices.
>
> It seems the place to do so might be in admin/widgets.py in the
> AutocompleteMixin.optgroups() method a few lines in where it says ...
>
>          choices = (
>              (obj.pk <http://obj.pk>,
> self.choices.field.label_from_instance(obj))
>              for obj in
> self.choices.queryset.using(self.db).filter(pk__in=selected_choices)
>          )
>
> Could a very kind person explain to me how I can hijack this and
> remove
> a couple of groups from the resulting choices?
>
> My use case is that I want members of the 'admin' group to be able to
> assign any group membershipto any other userexcept for a couple of
> groups which are too permissive. The excepted groups can only be
> assigned by a Superuser.
>
> Many thanks for any hints
>
> Cheers
>
> Mike
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to django-users...@googlegroups.com
> <mailto:django-users%2Bunsu...@googlegroups.com>.
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAP5HUWoZ14gH%3D8V1ARk-pnAKNWuVaqG%3D4%3DGSbwO_-0LrP7DFnw%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAP5HUWoZ14gH%3D8V1ARk-pnAKNWuVaqG%3D4%3DGSbwO_-0LrP7DFnw%40mail.gmail.com?utm_medium=email&utm_source=footer>.

Mike Dewhirst

unread,
Jan 5, 2020, 7:37:43 AM1/5/20
to django...@googlegroups.com
On 5/01/2020 11:42 am, Mike Dewhirst wrote:
> On 4/01/2020 9:03 pm, Integr@te System wrote:
>> Hi Mike,
>>
>> As we look at selected_choice result from iterating v over value
>> (line 428), option_value base on choice(line 438).
>> so we can limit input parameter of value variable from this def().
>
> selected_choices appear in the selected box on screen and choices
> appear in the available choices box.
>
> I can see how to tweak either of those if the method was my own. But
> it is an Admin method which interfaces with javascript to provide the
> user interface for making those selections.
>
> I think my original question was too brief.
>
> I'm thinking now I should be using my own version of AutocompleteMixin
> somehow and use formfield_for_manytomany when db_field == "groups" in
> my admin.py to get access to it.
>
> formfield_for_manytomany calls the AutocompleteSelectMultiple class
> which inherits AutocompleteMixin which has the optgroups() method
> which contains selected_choices and choices which are of interest to me.

Turns out the data I want to manipulate is encapsulated in widgets.py
FilteredSelectMultiple() where it returns context. That is called
directly from formfield_for_manytomany() so all I need to do is override
that in admin.py and tweak the context.

Thanks all who responded

Cheers

Mike
Reply all
Reply to author
Forward
0 new messages