dimanche 28 juin 2015

DJANGO ADDING NULL ON SAVE - IntegrityError at /****/ (1048, "***' cannot be null")

I am trying to save a record of an entity call 'Persona' using Django's save function but when I try to insert the new object I get a 1048 IntegrityError because for any reason Django is adding NULL on a field that can not be null and is foreign key (the field is GeneroID that is a foreign key of the table Genero), this during the insert statement even when I am passing the value correctly. The odd thing is that I have values on the 'Genero' table and I am capturing correctly the value passed on the POST even if I print the value I get the value passed but for any strange reason DJango is adding Null on GeneroID field.

I am using MySQL with InnoDB and Django 1.8. And my model is generated from a legacy DB.

MODEL GENERO

class Genero(models.Model):
id = models.AutoField(db_column='ID', primary_key=True)  # Field name made lowercase.
genero = models.CharField(db_column='Genero', max_length=50)  # Field name made lowercase.

class Meta:
    managed = False
    db_table = 'Genero'

MODEL PERSONA

class Persona(models.Model):
id = models.AutoField(db_column='ID', primary_key=True)  # Field name made lowercase.
nombre = models.CharField(db_column='Nombre', max_length=50)  # Field name made lowercase.
apellido1 = models.CharField(db_column='Apellido1', max_length=50)  # Field name made lowercase.
apellido2 = models.CharField(db_column='Apellido2', max_length=50)  # Field name made lowercase.
numeroidentificacion = models.CharField(db_column='NumeroIdentificacion', unique=True, max_length=50)  # Field name made lowercase.
observaciones = models.CharField(db_column='Observaciones', max_length=255)  # Field name made lowercase.
fechanacimiento = models.DateField(db_column='FechaNacimiento')  # Field name made lowercase.
direccionfoto = models.CharField(db_column='DireccionFoto', max_length=500)  # Field name made lowercase.
genero = models.ForeignKey(Genero, db_column='GeneroID',null=False)  # Field name made lowercase.
pais = models.ForeignKey(Pais, db_column='PaisID')  # Field name made lowercase.
lugarnacimiento = models.CharField(db_column='LugarNacimiento', max_length=100)  # Field name made lowercase.
direccion = models.ForeignKey(Direccion, db_column='DireccionID')  # Field name made lowercase.
profesion = models.CharField(db_column='Profesion', max_length=200)  # Field name made lowercase.
puesto = models.CharField(db_column='Puesto', max_length=200)  # Field name made lowercase.
lugartrabajo = models.CharField(db_column='LugarTrabajo', max_length=100)  # Field name made lowercase.

class Meta:
    managed = False
    db_table = 'Persona'

VIEW.PY

def guardarpersona(request):
direccion = json.loads(request.POST['direccion[]'])
persona = json.loads(request.POST['persona[]'])
temp = Direccion()
temp.descripcion = direccion[1]['value']
temp.distrito = Distrito.objects.get(pk=direccion[0]['value'])
temp.save()

p = Persona()
p.nombre = persona[1]['value']
p.apellido1 = persona[2]['value']
p.apellido2 = persona[3]['value']
numeroidentificacion = persona[4]['value']
observaciones = persona[12]['value']
fechanacimiento = persona[6]['value']
direccionfoto = ''
genero = Genero.objects.get(pk=persona[5]['value'])
print(genero)
pais = Pais.objects.filter(id=persona[8]['value'])
print(pais)
lugarnacimiento = persona[7]['value']
direccion = temp
profesion = persona[9]['value']
puesto = persona[10]['value']
lugartrabajo = persona[11]['value'] 

try:
    p.save()
except Exception:
    from django.db import connection
    print (connection.queries[-1])
return HttpResponse('Success!')      

DJANGO SQL STATEMENT BEEN GENERATED:

System check identified no issues (0 silenced). June 28, 2015 - 17:26:12 Django version 1.8.2, using settings 'WhiteKoala.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.

Genero object

Pais: Pais object

{'sql': "INSERT INTO Persona (Nombre, Apellido1, Apellido2, NumeroIdentificacion, Observaciones, FechaNacimiento, DireccionFoto, GeneroID, PaisID, LugarNacimiento, DireccionID, Profesion, Puesto, LugarTrabajo) VALUES ('ANDREW', 'FONSECA', 'RIVAS', '', '', NULL, '', NULL, NULL, '', NULL, '', '', '')", 'time': '0.000'} [28/Jun/2015 17:26:13]"POST /guardarpersona/ HTTP/1.1" 200 8

ERROR:

IntegrityError at /guardarpersona/ (1048, "Column 'GeneroID' cannot be null")

Also, here is a folder with some pics about my models, view.py, SQL statement been generated and the error: http://ift.tt/1TXJQ65

Any idea about what I am doing wrong? I am new on Django and I would appreciate help and recommendations.

Aucun commentaire:

Enregistrer un commentaire