3

So I have this structure where I have multiple abstract Django models and all my models inherit one or more of these abstract models, for instance

class BaseProblem(Base, Slugged, Ownable, Tagged)

Now a situation has so arisen that I need to make another abstract class which derives from all these classes and in that I have to write a save function that uses one of the field which is defined in the other abstract classes

class Slugged(models.Model):
    class Meta:
        abstract = True
    title = models.CharField()
    slug = models.SlugField()

    def save(self, *args, **kwargs):
        # generate slug

class BaseProblem(Base, Slugged, Ownable, Tagged):
    class Meta:
        abstract = True
    def save(self, *args, **kwargs):
        if not self.id:
            # do something with slug field

But the issue is that when the save function for the XYZ class is executed, the slug field does not exist, because the slug field is not yet generated.

I am assuming the save function for abstract classes are being executed in alphabetical order that's why the slug field does not exist at the time the save function for BaseProblem is executed

4

0 回答 0