How to use a model that does not have a Primary Key?

I have a table called “Order Detail” that does not contain PK
I want it to show all the products in the order. but before doing that I wanted to perform a Print and I got the following error:


class Pedido(models.Model):  # Order
    cod_pedido = models.AutoField(primary_key=True)
    user= models.CharField(max_length=50)
    class Meta:
        managed = False
        db_table = 'pedido'
class Detalle_pedido(models.Model): # Order detail
    pedido_id = models.ForeignKey(Pedido, db_column="pedido_id", on_delete=models.CASCADE)
    cod_art = models.ForeignKey(Stock, db_column="cod_art", on_delete=models.CASCADE)
    cant_pedida = models.IntegerField()
    class Meta:
        managed = False
        db_table = 'detalle_pedido'

OperationalError at /stock/verpedido/1/

(1054, “Unknown column ‘’ in ‘field list’”)

Request Method: GET
Request URL:
Django Version: 4.2.5
Exception Type: OperationalError
Exception Value: (1054, “Unknown column ‘’ in ‘field list’”)
Exception Location: C:\Users\marti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\MySQLdb\, line 255, in query
Raised during: stock.views.verPedido
Python Executable: C:\Users\marti\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\python.exe
Python Version: 3.9.13
Python Path: [‘C:\Users\marti\OneDrive\Documentos\GitHub\testDjango\InsumoTestCasa’, 'C:\Program ’ ‘Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\’, 'C:\Program ’ ‘Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\DLLs’, 'C:\Program ’ ‘Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib’, ‘C:\Users\marti\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0’, ‘C:\Users\marti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages’, 'C:\Program ’ ‘Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0’, 'C:\Program ’ ‘Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\site-packages’]
Server time: Sun, 05 Nov 2023 23:25:47 +0000

How can I solve this problem or how can I use my table without the need to add an auto-incremental id.

If you are using the model as “read-only”, and avoid any reference to the pk alias, you can generally select any arbitrary field as the PK.

If you’re looking to update the data in the model, you must have a primary key.

From the docs at Models | Django documentation | Django

Each model requires exactly one field to have primary_key=True (either explicitly declared or automatically added).

I want to use that table to show the products that were placed in the order. I don’t want to update or delete. This is the code I want to run (for now)

def verPedido(request, cod):
    contexto= {}
    print(f"Se recibio del get el codigo={cod}")
    print(Detalle_pedido.objects.all()) #ERROR HERE
    return render(request, 'mostrarPedido.html', contexto)

For example:
I received 1 order where they ordered 3 products, I only want to be able to see the products (2 pens, 3 folders, 2 boxes of clips)

So put the primary_key attribute on the cant_pedita field. (Again, this should be ok provided you make no attempt to update the data.)

Also, your query is going to return all instances of Detalle_pedido, not just the ones related to a specific Pedido.