I want to disable automatic id creation in Django Models. Is it possible to do so? How?
Asked
Active
Viewed 9,146 times
8
-
Declare a primary key on your model (https://docs.djangoproject.com/en/1.8/ref/models/fields/#primary-key). Models without IDs aren't supported (AFAIK) – Roba Oct 17 '15 at 22:12
-
Yeah that's possible. But what if I just want to store the like of a user for an item? I don't need id for that stuff. – The Coder Oct 18 '15 at 08:48
1 Answers
6
As mentioned in the reply, you need to declare a primary key on a non-AutoField. For example:
from django.db import models
class Person(models.Model):
username = CharField(primary_key=True, max_length=100)
first_name = CharField(null=True, blank=True, max_length=100)
Please note, setting a field to primary_key=True
automatically makes it unique and not null. Good luck!

FlipperPA
- 13,607
- 4
- 39
- 71
-
Yeah that's possible. But what if I just want to store the like of a user for an item? I don't need id for that stuff. – The Coder Oct 18 '15 at 08:48
-
Right, you may have use-cases where in your database tables you wouldn't go with 3rd form of normalisation, but if you need those tables to be exposed in Django as models, you have to. There is little / no loss from your side, these fields can be auto-managed and you would hardly argue about the used space ... – Roba Oct 18 '15 at 10:03
-
It isn't even really exposed to you as a coder unless you view it in the database, and minuscule overhead for a lot of reliability. Having a guaranteed unique key in a DB has saved my butt over the years more times than I can count. – FlipperPA Oct 18 '15 at 10:23