Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable automatic id creation in Django?

I want to disable automatic id creation in Django Models. Is it possible to do so? How?

like image 429
The Coder Avatar asked Oct 17 '15 15:10

The Coder


1 Answers

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!

like image 103
FlipperPA Avatar answered Oct 18 '22 17:10

FlipperPA