openwisp-users icon indicating copy to clipboard operation
openwisp-users copied to clipboard

[admin] Make organization selection automatic when only 1 org

Open nemesifier opened this issue 5 years ago • 5 comments

When the user logged in has access to only one organization, the organization select should automatically set that org as the default value, the user should not need to select it.

nemesifier avatar Aug 23 '20 22:08 nemesifier

Update:

this could do the trick: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_choice_field

if len(request.user.organizations_managed) <= 1:
    kwargs['initial'] = request.user.organizations_maanged[0]

I have not tested this.

nemesifier avatar Aug 25 '20 03:08 nemesifier

I think that way would require code in all ModelAdmin where the model uses organization field.

I'm testing with the field, something like adding a default value to the field https://github.com/openwisp/openwisp-users/blob/master/openwisp_users/mixins.py#L84

Let me think about your suggestion, I didn't knew about organizations_managed but its a good point.

pniaps avatar Aug 25 '20 12:08 pniaps

I think that way would require code in all ModelAdmin where the model uses organization field.

Not exactly. All the modules which support multitenancy inherit this mixin: https://github.com/openwisp/openwisp-users/blob/master/openwisp_users/multitenancy.py#L11

So it should be enough to implement it once for every multi-tenant admin.

nemesifier avatar Aug 25 '20 16:08 nemesifier

Great, I will try it. Thanks.

pniaps avatar Aug 25 '20 17:08 pniaps

Well done.

keshavgbpecdelhi avatar Jun 22 '21 10:06 keshavgbpecdelhi

Implemented in https://github.com/openwisp/openwisp-users/issues/333.

nemesifier avatar Jan 23 '23 17:01 nemesifier