I have a Posts
model. And field Order = models.PositiveIntegerField()
has been created for arbitrary sorting.
class Post(models.Model):
title = models.CharField(max_length=15)
create = models.DateTimeField(auto_now_add=True)
Order = models.PositiveIntegerField()
Objective: in the model, overriding the save
method, do add the index +1 (from the last available index of Posts) to this field when adding each new Post
.
That is, each post must have an Order-index, and if there are already 3 posts on the site, then when the fourth is added - index 4 is added to his the field, and so on.
"1" index in Order field (1st post), "2" index in Order field (2nd post) etc 3, 4, 5... similar ID.
Help implement this logic im method save
. It seems simple, but I don't know how to approach it.
I understand that in the model and what I do:
def save(self, *args, **kwargs):
qs = self.order.objects.all()
last_item = qs.latest(self.order)
last_item.order += 1
super().save(*args, **kwargs)
But this don't work. Help me, please!