When making ID in django

Asked 2 years ago, Updated 2 years ago, 38 views

Hello, everyone When defining the Django model field Define the primary key id = models.IntegerField() Is there a way to make the ID number automatically increase per row like AutoField?

django python mysql

2022-09-21 21:13

1 Answers

For your information, Django automatically creates the id field when creating the model. You don't have to create an id field in the model, but after you migrate, the model name.Available as id.

If you make it

id = models.AutoField(primary_key=True)

You can use it

class Users(models.Model):
    something = models.PositiveIntergerField(unique=True, default=number)

    def number(self):
        num = Users.objects.count()
        if num == None:
            return 1
        else:
            return num + 1

You can do it with that.


2022-09-21 21:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.