Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django models.Model superclass

Tags:

python

django

I would like to create a models.Model class that doesn't became part of the database but just an interface to other models (I want to avoid repeating code).

Something like that:

class Interface(models.Model):
    a = models.IntegerField()
    b = models.TextField()

class Foo(Interface):
    c = models.IntegerField()

class Bar(Interface):
    d = models.CharField(max_length='255')

So my database should have only Foo (with a,b,c collumns) and Bar (with a,b,d) but not the table Interface.

like image 302
JBernardo Avatar asked Dec 17 '22 08:12

JBernardo


1 Answers

"Abstract base classes"

Abstract base classes are useful when you want to put some common information into a number of other models. You write your base class and put abstract=True in the Meta class. This model will then not be used to create any database table. Instead, when it is used as a base class for other models, its fields will be added to those of the child class.

like image 166
Ignacio Vazquez-Abrams Avatar answered Dec 31 '22 07:12

Ignacio Vazquez-Abrams