Question

I wonder - is it possible to inherit classes like this?

For exmple, i have 2 abstract classes:

    class Book(models.Model):
        name = models.TextField()

        class Meta:
            abstract = True

    class Page(models.Model)
        num = models.IntegerField()
        book = models.ForeignKey('Book')

        class Meta:
            abstract = True

And so - I want to make inherited classes for these two, let them be BigBook and BigBookPage

But if i do so, python says to me, that my FK field can't have a relation with an abstract model. And i can't find a way to redefine FK in inherited models. So do i have to ONLY create foreign keys in the inherited models - not the parents?

And if i have same model methods, that use foreign keys, defined in the parent models... i have to move them to every child - so they could use their foreign keys?

Was it helpful?

Solution

It sounds like you want to mark Book as proxy=True. See proxy models. It will create a book model, but also let you have other models that inherit from it letting you customize the functionality of the subclasses.

class Book(models.Model):
    class Meta:
        proxy = True

class BigBook(Book):
    # BigBook properties go here.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top