[Django]-Overriding the save() method of a model that uses django-mptt

7👍

Solved this with a small addition to my custom save method. Had to change it from

def save(self, *args, **kwargs):
    self.url = self.get_absolute_url()
    super(Page, self).save(*args, **kwargs)

to this:

def save(self, *args, **kwargs):
    if not self.id:
        Page.tree.insert_node(self, self.parent)
    self.url = self.get_absolute_url()
    super(Page, self).save(*args, **kwargs)

0👍

changing the order should also work, since the object should get an id upon saving and the save()-method isn’ returning anything!

def save(self, *args, **kwargs):
    super(Page, self).save(*args, **kwargs)    
    self.url = self.get_absolute_url()

Leave a comment