我正在努力解决这个(希望如此)简单的问题。
我正在使用 Django-nonrel,并且正在尝试设置一些模型。我简化了下面的代码:
class Application(models.Model)
name = "Application"
def get_name(self):
print(self.name)
class ExampleApp(Application)
name = "Example Application"
class Site(models.Model)
app = models.OneToOneField(Application, null=True)
id = models.CharField(max_length=1)
# other details not important
当我获得 的实例时Site
,我想获得派生类的实例(例如ExampleApp
)
class MyView(View):
def get(self, request, *args, **kwargs):
# Presuppose that I have an object with id="a"
site = Site.objects.filter(id="a")
#<Application: Application>
app = site.app
#returns "Application", not "Example Application"
app.get_name()
如何访问派生类?
(理想情况下,我想做Application
一个抽象基类,但是 django 不允许与抽象基类建立关系。我会在里面定义字段Application
,但是 django-nonrel 不支持多表继承。)