Fix initial roles in user form

This commit is contained in:
Ales (Shagi) Zabala Alava 2021-05-12 16:10:27 +02:00
parent 7c1cd322ed
commit 873e8cc043
1 changed files with 3 additions and 2 deletions

View File

@ -25,6 +25,7 @@ class UserForm(forms.ModelForm):
label=_('roles'),
required=False,
)
class Meta:
model = User
fields = (
@ -33,8 +34,8 @@ class UserForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if 'roles' not in self.initial and self.instance.pk is not None:
self.initial['roles'] = self.instance.user_roles.values_list('role', flat=True)
if self.instance.pk is not None:
self.initial['roles'] = list(self.instance.user_roles.values_list('role', flat=True))
def save(self, commit=True):
obj = super().save(commit=False)