Django工作流将模型超类转换为子类 [英] Django workflow to convert model superclass to subclass

查看:97
本文介绍了Django工作流将模型超类转换为子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Django项目,其中有两个模型:申请人和客户端,其中客户端是申请人的子类。我想要一种允许用户将现有的申请人实例添加为客户端的方法。我已经有了申请人实例的观点,所以我认为在该页面上有一个客户端模型表单会这样做,但是从文档,它不像您可以使用超类的实例初始化模型窗体。我知道我可以通过将代码直接传递到数据库并向子类表中添加一个字段来实现,但是还有更多的Django-y方法吗?

I have a Django project with two models: Applicant and Client, where Client is a subclass of Applicant. I would like some way of allowing a user to add an existing Applicant instance as a Client. I already have a view for Applicant instances, so I thought that having a Client model form on that page would do this, but from the documentation it does not look like you can initialize a model form with an instance of a superclass. I know I could do this by having code that goes directly to the database and adds a field to the subclass table, but is there a more Django-y way of doing it?

推荐答案

您可以从现有的申请人实例中创建一个客户端以下代码:

You can create a Client instance from an existing Applicant instance with the following code:

client = Client(applicant_ptr=applicant)
client.save_base(raw=True)

这篇关于Django工作流将模型超类转换为子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆