Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create model in Django without automatic ID?

I need a table without a primary key (in Django it was created automatically). So my question is: Can I create a model without an ID/primary key?

I'm using Django 1.7.

like image 307
HQM Avatar asked Feb 14 '15 13:02

HQM


People also ask

Does Django automatically create ids for models?

Usually, Django automatically adds an integer id field to every model, this acts as an auto-incrementing primary key for every object of the model.

Does every Django model have an ID?

By default, Django adds an id field to each model, which is used as the primary key for that model.

What is the difference between ID and PK in Django?

pk is the attribute that contains the value of the primary key for the model. id is the name of the field created as a primary key by default if none is explicitly specified.


1 Answers

You can create a model without an auto-incrementing key, but you cannot create one without a primary key.

From the Django Documentation:

If Django sees you’ve explicitly set Field.primary_key, it won’t add the automatic id column.

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

like image 189
Selcuk Avatar answered Oct 06 '22 14:10

Selcuk