假设我有两个模型:
class Topic(models.Model):
    title = models.CharField()
    # other stuff
class Post(models.Model):
    topic = models.ForeignKey(Topic)
    body = models.TextField()
    # other stuff
我想创建一个包含两个字段的表单:Topic.title和Post.body。当然,我可以创建以下表单:
class TopicForm(Form):
    title = forms.CharField()
    body = forms.TextField()
    # and so on
但我不想重复代码,因为我已经有了title模型body。我正在寻找这样的东西:
class TopicForm(MagicForm):
    class Meta:
        models = (Topic, Post)
        fields = {
            Topic: ('title', ),
            Post: ('body', )
        }
    # and so on
另外,我想在基于类的视图中使用它。我的意思是,我想把视图写成:
class TopicCreate(CreateView):
    form_class = TopicForm
    # ...
    def form_valid(self, form):
        # some things before creating objects
正如评论中所建议的,我可以使用两种形式。但在我看来,我没有看到任何使用两种表单的简单方法TopicCreate——我应该重新实现所有属于获取表单的方法(至少)。
所以,我的问题是:
Django 中是否已经针对我的要求实现了某些功能?还是有更好(更简单)的方法?
或者
您知道在基于类的视图中使用两种表单的简单方法吗?如果是这样,请告诉我,它也可以解决我的问题。