[Django]-Add number field to Django model

3👍

You can use something like this, but it can be resource consuming if you do not want to use the default id field.

class Film(models.Model):
    def number():
        no = Film.objects.count()
        return no + 1

    movie_row = models.IntegerField(unique=True,default=number)

0👍

From Django’s Documentation:

By default, Django gives each model the following field:

id = models.AutoField(primary_key=True)

This is an auto-incrementing primary key.

If you’d like to specify a custom primary key, just specify primary_key=True on one of your fields. 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).

If you want yours to explicitly be called movie_id you probably need something like:

class Film(models.Model):
    movie_id = models.AutoField(primary_key=True)

Leave a comment