Home > Software design >  django error foreign key Cannot assign must be a instance
django error foreign key Cannot assign must be a instance

Time:08-28

I am creating a stored procedure, I need to create a user in django, I have a foreign key called cargo_empleado that causes me an error

my models.py

class CargoEmpleado(models.Model):
nombre_cargo = models.CharField(max_length=50, blank=True, null=True)

class Meta:
    managed = False
    db_table = 'Cargo_empleado'

class Empleado(models.Model):
rut = models.CharField(primary_key=True, max_length=9)
nombres = models.CharField(max_length=100, blank=True, null=True)
apellidos = models.CharField(max_length=100, blank=True, null=True)
correo_electronico = models.CharField(max_length=100, blank=True, null=True)
usuario = models.CharField(max_length=50, blank=True, null=True)
contrasena = models.CharField(max_length=255, blank=True, null=True)
activo = models.IntegerField()
cargo_empleado = models.ForeignKey(CargoEmpleado, models.DO_NOTHING, db_column='cargo_empleado')
id_empresa = models.ForeignKey('Empresa', models.DO_NOTHING, db_column='id_empresa', blank=True, null=True)
id_unida = models.ForeignKey('UnidadInterna', models.DO_NOTHING, db_column='id_unida')

class Meta:
    managed = False
    db_table = 'Empleado'

my views.py

def crearusuario(request):
if request.method=="POST":
    if request.POST.get('rut') and request.POST.get('nombres') and request.POST.get('apellidos') and request.POST.get('correo_electronico') and request.POST.get('usuario') and request.POST.get('contrasena') and request.POST.get('activo') and request.POST.get('cargo_empleado') and request.POST.get('id_empresa') and request.POST.get('id_unida'):
        usersave= Empleado()
        usersave.rut=request.POST.get('rut')
        usersave.nombres=request.POST.get('nombres')
        usersave.apellidos=request.POST.get('apellidos')
        usersave.correo_electronico=request.POST.get('correo_electronico')
        usersave.usuario=request.POST.get('usuario')
        usersave.contrasena=request.POST.get('contrasena')
        usersave.activo=request.POST.get('activo')
        usersave.cargo_empleado=request.POST.get('cargo_empleado')
        usersave.id_empresa=request.POST.get('id_empresa')
        usersave.id_unida=request.POST.get('id_unida')
        cursor=connection.cursor()
        cursor.execute("call SP_crear_usuario('" usersave.rut "','" usersave.nombres "', '" usersave.apellidos "', '" usersave.correo_electronico "', '" usersave.usuario "', '" usersave.contrasena "', '" usersave.activo "', '" usersave.cargo_empleado "', '" usersave.id_empresa "', '" usersave.id_unida "')")
        messages.success(request, "El empleado " usersave.nombres " se guardo correctamente ")
        return render(request, 'app/crearusuario.html')
else:
    return render(request, 'app/crearusuario.html')

error: Cannot assign "'funcionario'": "Empleado.cargo_empleado" must be a "CargoEmpleado" instance.

Please someone help me!

CodePudding user response:

You are trying to assign the id to the ForeignKey field. Instead, get the object with that id:

        usersave.cargo_empleado=CargoEmpleado.objects.get(id=int(request.POST.get('cargo_empleado')
        usersave.id_empresa=Empresa.objects.get(id=int(request.POST.get('id_empresa')))
        usersave.id_unida=UnidadInterna.objects.get(id=int(request.POST.get('id_unida')))

CodePudding user response:

You can use the id as well. Each ForeignKey field comes with a _id field that stores the foreign key object id as integer. So you should be able to do this:

usersave.cargo_empleado_id = request.POST.get('cargo_empleado')
  • Related