Update user data trough custom form in Django/Python

im currently working with Python 3.4.3 and Django 1.8.4, and i need to update some values from users, this is my code. When i try to save the user doesnt save the new permission state.

def permissions(request):
if request.method == 'POST':
    try:
        username = request.POST.get('usuario')
        permiso = request.POST.get('permiso')

        print("usuario seleccionado", username)
        print("permisos seleccionados", permiso)

        if permiso == 'Administrador':
            permiso = 'ad'
            print("seleccion permisos de Administrador")
        if permiso == 'Planificador':
            permiso = 'pl'
            print("seleccion permisos de Planificador")
        if permiso == 'Básico':
            permiso = 'bs'
            print("seleccion permisos de Básico")

        usp = UserProfile(user = username, role = permiso) 
        print("Usuario modificado",usp)
        usp.save()

        return HttpResponse(
        json.dumps({'redirect': "/SQM/administrar_usuarios/"}),
        content_type="application/json"
                    )         
    except:
        return HttpResponse(
                    json.dumps({'redirect': "error!"}),
                    content_type="application/json"
                                )

i send the data via post with javascript. This is my model.

class UserProfile(models.Model):
user = models.OneToOneField(User)
role = models.CharField(max_length=2, choices=TIPOS_USUARIO)
class Meta:
    verbose_name='Perfil de usuario'
    verbose_name_plural = 'Perfiles de usuario'
def  __str__(self):
    return self.user.username

This is my view from the html file.

<div class="col-md-4">
                <label>Seleccionar Usuario</label>
                <select class="form-control usuarios" id="uid" >
                    {% for usuarios in users%}
                    <option data-userdata-id="{{ usuarios.id }}">{{usuarios.user}}</option>
                    {%endfor%}
                </select>
        </div>
        <div class="col-md-4">
            <div class="form-group">
                <label>Seleccionar Permisos</label>
                <select class="form-control permisos" id="pid">
                    {% for roles in permisos%}
                    <option data-permisos-id="{{ roles }}">{{roles}}</option>
                    {%endfor%}
                </select>                   
            </div>
        </div>

I am trying to update the role state from 1 user.

My decorator for custom permissions.

def permission_required(permission_array):
def decorator(view_function):
    def wrapper(request, *args, **kwargs):
        if(request.user.userprofile.role in permission_array):
            return view_function(request, *args, **kwargs)
        else:
            return HttpResponse("<h2>No cumple permisos</h2>")
    return wrapper
return decorator

Thanks for your wisdom.

Answers


When i was tryig to rename the user permissions also i was trying to re-save the same user, and thats not allowed because my model is one to one field.

i replace this line.

usp = UserProfile(user = username, role = permiso)

whith this one.

        user = User.objects.get(username=username)            
        usp = UserProfile.objects.get(user = user)            
        usp.role = permiso
        usp.save()

Thank you so much for your comments.


Need Your Help

import djcelery getting error

python django djcelery

When importing djcelery i am getting the below error. Is there any other method to install?