I am a beginner in django and trying to create a web application for my task. I am trying to assign a roles to every to user. But i have an error. enter image description here
My model
class Rol(Group):
state = models.BooleanField(default=True)
description = models.CharField(max_length=200)
permission = models.ManyToManyField(Permission)
def __str__ (self):
return '{}'.format(self.state,self.description, self.permission)
In this part i don't have any problem the permission is asigned to rol
But here My model:
class User(AbstractUser):
state = models.BooleanField(default=True)
roles = models.ManyToManyField(Rol)
def __str__ (self):
return '{}'.format(self.username,self.state,self.roles)
My form:
class UserForm(UserCreationForm):
username = forms.CharField(label="User",widget=forms.TextInput(attrs=
{"class":"form-control"}))
password1 = forms.PasswordInput()
password2 = forms.PasswordInput()
state = forms.CheckboxInput()
roles =forms.ModelMultipleChoiceField(label="Roles",queryset=Group.objects.all(),
widget=forms.CheckboxSelectMultiple)
class Meta:
model = User
fields = [
"username",
"password1",
"password2",
"activo",
"roles"
]
In roles lists the roles created but not assigned to the user at the time of saving, it shows me an error:
- Field 'id' expected a number but got <Group: standart>.
- The above exception (int() argument must be a string, a bytes-like object or a real number, not 'Group') was the direct cause of the following exception:
And highlight this line
- form.save_m2m()
My view
def create_user(request):
if request.method == 'POST':
form = UserForm(request.POST)
if form.is_valid():
form2=form.save(commit=False)
form2.save()
form.save_m2m()
return redirect('list_user')
return render(request, 'user/user_form.html',{'form': form})
else:
form = UserForm()
return render(request, 'user/user_form.html',{'form': form})
OBS: But in my user form when changing the queryset of roles: Group by Rol, it assigns the roles but it does not list the names, only the states