I have defined this abstract base model as below:
class ActivityAbstractBaseModel(models.Model):
POOR = 'PR'
FAIR = 'FA'
MEDIOCRE = 'ME'
GOOD_ENOUGH = 'GE'
GOOD = 'GO'
VERY_GOOD = 'VG'
EXCELLENT = 'EX'
STATE = [
(POOR, 'Poor'),
(FAIR, 'Fair'),
(MEDIOCRE,'Mediocre' ),
(GOOD_ENOUGH, 'Good Enough' ),
(GOOD, 'Good'),
(VERY_GOOD, 'Very Good'),
(EXCELLENT, 'Excellent'),
]
speaking = models.CharField(max_length=50, choices=STATE, default=GOOD)
I then inherit this abstract model as below and added the new field writing
class Fluency(ActivityAbstractBaseModel):
writing = models.CharField(max_length=50, choices=STATE, default=GOOD)
Now, this new field writing
is trying to access the variable GOOD
and STATE
that was created in the abstract class but I am having the NameError
exception. Is there a way to get these variables?
Although your class will inherit those class variables but the code you cannot directly refer them where you define your class (because they don't exist in that scope). Instead you can refer to them as ActivityAbstractBaseModel.GOOD
, etc. inside your class declaration.
class Fluency(ActivityAbstractBaseModel):
writing = models.CharField(max_length=50, choices=ActivityAbstractBaseModel.STATE, default=ActivityAbstractBaseModel.GOOD)
print(Fluency.GOOD) # This works properly