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