[Answer]-How to checked permissions in edit user form?

1👍

Use the argument data to populate the checkboxes when you instantiate the form (only outside the POST request) like this:

profile_user = User.objects.get(pk=user_id)

if request.method == 'POST':
    form = UserForm(request.POST, instance=profile_user)
    if form.is_valid():
        form.save()
        [... here will save permissions ...]
        return HttpResponseRedirect(reverse('home'))
else:
    user_permissions_list = # place here the query to get the profile_user permissions
    form = UserForm(instance=profile_user, data={'permissions':user_permissions_list})

Using data is the key to mark the checkboxes according to user actual permissions.

Hope it helps!

Leave a comment