I’m learning to use models with relationships to other tables
class Stock(models.Model): #Modelo del stock
cod_art = models.IntegerField(primary_key=True)
descripcion = models.CharField(max_length=50)
stock = models.IntegerField()
class Meta:
managed = False
db_table = 'stock'
class Pedido(models.Model): #Modelo para los pedidos
cod_pedido = models.AutoField(primary_key=True)
#cod_detalle = models.IntegerField()
cod_detalle = models.ForeignKey(Stock, on_delete=models.CASCADE)
user= models.CharField(max_length=50)
class Meta:
managed = False
db_table = 'pedido'
I have these 2 tables: “Orders” and “Stock”. My problem is when creating an order, I want to store the product ID and when performing the .save() I get the following error
(1054, “Unknown column ‘cod_detalle_id’ in ‘field list’”)
The code he made to save it in the database is this:
s = Stock(cod_art = 8 , descripcion = "lapicera verde", stock = 10)
s.save()
p = Pedido.objects.create( cod_detalle = s , user = 44 )