Skip to content Skip to sidebar Skip to footer

Django How To Access An Abstract Base Model Local Variables

I have defined this abstract base model as below: class ActivityAbstractBaseModel(models.Model): POOR = 'PR' FAIR = 'FA' MEDIOCRE = 'ME' GOOD_ENOUGH = 'GE' GOOD

Solution 1:

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

Post a Comment for "Django How To Access An Abstract Base Model Local Variables"