How do I manipulate this method to be applied only on new entries and does not have a previous entry.
When in admin, I will add an record, this method is applied correctly. but when I edit the same record, this method is applied again.
def save(self):
p = Product.objects.last()
if p:
self.productnumber = 1000 + p.id
else:
self.productnumber = 1000
super(Product, self).save()
If you want to execute some code in the save() method, only for non-existing items you should check if a primary key exists.
def save(self):
if self.pk is None:
# Code for new objects
else:
# Code for existing objects