Here's what I was using:
class a(models.Model):
x = models.CharField()
class b(a):
pass
The problem with this is that when an instance of b is created, an instance of a is also created, I'm guessing this is because b is inheriting some property that Django assigns such as the database table. I would like to have b have all the fields and methods so that this duplication does not happen. How can this be done without simply copy and pasting all the code from a to b or using an abstract base class c and have a and b both inherit from c (I'd like to only have two models/classes)? Would you have to use metaclasses?
class A(models.Model):
#some fields here
x = models.CharField()
class Meta:
abstract = True
class B(A):
pass
A will be an abstract class, and you cannot use this class alone. But as I understood, you want to have two real classes A and B. In this case you need a third (abstract) class C. So they will inherit the fields from the abstract one and add extra fields to them.
For example: suppose that abstract is C
class C(models.Model):
# the common fields
class Meta:
abstract = True
class A(C):
#extra fields if you need or pass
class B(C):
#extra fields if you need or pass
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With