0

I have these tables:

class OpeDatos(models.Model):
    id_dato = models.IntegerField(primary_key=True)
    id_usuario = models.ForeignKey(SisUsuarios, db_column='id_usuario')
    id_region = models.ForeignKey(SisRegiones, db_column='id_region')
    titulo = models.CharField(max_length=70, blank=True)
    class Meta:
        managed = False
        db_table = 'ope_datos'

class OpeProductos(OpeDatos):
    id_producto = models.IntegerField(primary_key=True)
    iddato = models.OneToOneField(OpeDatos, primary_key=True, db_column="id_dato",   parent_link=True)
    id_producto_tipo = models.ForeignKey(DefProductosTipos, db_column='id_producto_tipo')
    class Meta:
         managed = False
         db_table = 'ope_productos'

I want to insert data :

from apps.inicio.models import SisUsuarios, SisRegiones, OpeDatos

usuario = SisUsuarios.objects.get(pk=1)
region = SisRegiones.objects.get(pk=1)
datos = OpeDatos()
datos.id_usuario = usuario
datos.id_region = region     
datos.save()
producto = OpeProductos() 
producto.iddato = datos.id_dato
producto.save()

displays this message:

ValueError at /productos/add/

Cannot assign None: "OpeProductos.iddato" does not allow null values.

can you help me, please.

1
  • Why do you have OpeProductos inheriting from OpeDatos? That is certainly not what you want to do, especially as firstly there is a OneToOne field that relates them and secondly they are unmanaged models. Commented Jan 8, 2014 at 14:53

2 Answers 2

1

When creating an id manually you should use AutoField instead of IntegerField

id_dato = models.AutoField(primary_key=True)

https://docs.djangoproject.com/en/1.6/ref/models/fields/#autofield

What is happening is that since you are not explicitly defining the 'datos' object's id it doesnt have one, and then producto complains because the key can't have an empty value.

AutoField should fix this

Sign up to request clarification or add additional context in comments.

1 Comment

I made the change. Displays the following message : IntegrityError at /productos/add/ (1452, 'Cannot add or update a child row: a foreign key constraint fails (panuncios.ope_datos, CONSTRAINT fk_ope_datos_id_usuario FOREIGN KEY (id_usuario) REFERENCES sis_usuarios (id_usuario) ON DELETE NO ACTION ON UPDATE NO ACTION)')
0

I am surprised it doesn't fail at the earlier line: datos.save() because you have not supplied a value for the primary key datos.id_dato

Normally in Django you would need to use an AutoField to get an auto-incrementing primary key.

Also you should not be specifying primary_key=True on the OpeProductos.iddato field, you can only have one primary key per model.

Then the error you are seeing is due to the fact that datos.id_dato is None since you did not provide any value for it before saving the datos instance.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.