[Fixed]-Missing auto generated id in django model

0đź‘Ť

âś…

Thanks everyone for the feedback and the time+effort.

At last after a lot of try-error I found the solution

It seems that even if you have an autogenerated id that is not part of your model you have to pass it by parameter anyway, and as like normal SQL it can be done by passing a NULL value or the Python equivalent None

So, the answer is

Rose = Producto.objects.get(id=124105)
cot = Cotizacion(None, 13, 141, 15, Rose)
cot.save()

1đź‘Ť

This is happening because the ForeignKey field does not allow null values by default, but you’re trying to initialize an instance of the Cotizacion class without a reference to any Producto instance.

0đź‘Ť

You’re trying to initialize Cotization with three arguments where definition of this model expects five of them.

Are we talking still about Cotization or maybe Producto?

Update (after original post was updated about additional data):
Please take a look on this link. Please use underscore character or just full object. “dot” notation is same optimal as giving full object.

cot = Cotizacion(minimo, maximo, promedio, cantidad, producto_id)

or

cot = Cotizacion(minimo, maximo, promedio, cantidad, producto)

Leave a comment