I've got an abstract model and a load of subclasses that inherit from it.
I can get a list of these subclasses but how would I go about putting them in a form so that the user can choose to create one of them?
So, for instance:-
class Abstract(models.Model):
class Meta:
abstract = True
class ImplA(Abstract):
pass
class ImplB(Abstract):
pass
I need a form which enables the user to choose to create either an instance of ImplA or an instance of ImplB.
答案 0 :(得分:0)
您可以按照以下方式实施
首先创建一个模型来保存用户输入
SUBCLASSCHOICE = (
('ImplA', 'ImplA'),
('ImplB', 'ImplB'),
)
class UserTable(models.Model):
abstract_class = models.CharField()
sub_class = models.CharField(choices=SUBCLASSCHOICE)
您现在可以从UserTable获取数据并动态生成类,如下所示
models.py user_table = UserTable.objects.get()
local()[user_table.sub_class] = type(
user_table.sub_class, #model class name
(user_table.abstract_class ,),
)