I would like to set a limit of the number of rows in certain models. to implement this, when I add a new row to the database in django, I would like to delete the old row and renumber id(pk).
By referring to the following links about the above issue, I was able to set the number of lines limit and delete old lines.
Limit the number of rows in a Django table
However, this solution, when the old row deleted, id(pk) is also deleted. if I set limit number of rows is 100, the row id after deletion database is starting at 101. To solve this issue, I would like to renumber id as starting 1 when deleting old id. Could you tell me how to implement it?
class History(models.Model):
history = models.IntegerField(default=0)
def save(self):
objects = History.objects.all()
print(id)
if objects.count() > 100:
objects[0].delete()
self.save()
class Player(models.Model):
user = models.CharField(max_length=255)
game_count = models.IntegerField(default=0)
medal = models.IntegerField(default=100, verbose_name='medal')
game_history = models.ForeignKey(History, on_delete=models.CASCADE,
verbose_name='game_history', null=True)
def __str__(self):
return self.user