'How to have consecutive numbers in django model

I have a model that represents a book and a model named chapter.

models.py

class Book(models.Model):
    title = models.CharField(max_length=255)
    book_summary = models.TextField()
    author = models.ForeignKey(Author, related_name='books', on_delete=models.DO_NOTHING)
    upload_date = models.DateField(auto_now_add=True)
    tags = models.ManyToManyField(Tag, related_name='books', blank=True)
    genre = models.ForeignKey(Genre, related_name='books', on_delete=models.PROTECT,)

    def __str__(self):
        return self.title


class Chapter(models.Model):
    book = models.ForeignKey(Book, related_name='chapters', on_delete=models.CASCADE)
    chapter_num = # has to be consecutive
    text = models.TextField()

I want to number each chapter so that I can order them. But I cannot implement that with AutoField because if I delete a chapter there will be 1, 3 ,4... and so on. How can I do this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source