在Django 1.5中以一种形式使用两个模型 [英] Using two models in one form in django 1.5
问题描述
如何在Django 1.5中使用CreateView以一种形式使用具有OneToOne关系的两个模型?
How can I use two models with OneToOne relation in one form using the CreateView in Django 1.5?
我的模型是这些:
class Act(models.Model):
name = models.CharField()
class DetailAct(models.Model):
detail = models.CharField()
act = models.OneToOneField(Act)
我的表格
class ActForm(forms.ModelForm):
name = forms.CharField(widget=forms.TextInput())
class Meta:
model = models.Act
class DetailActForm(forms.ModelForm):
detail = forms.CharField(widget=forms.TextInput())
class Meta:
model = models.DetailAct
谢谢
推荐答案
您可以在一个< form>
标记中使用两个 Form
对象,而不会出现问题.只需确保在视图中将 prefix ="form-1"
传递给其中一种形式(或两者-只要前缀不同)即可.有关示例,请参见此答案.
You can use two Form
objects in one <form>
tag without problems. Just make sure that you pass prefix="form-1"
to one of the forms (or both - as long as the prefixes are different) in your view. See this answer for an example.
不是,您不能为此使用基于内置类的视图.或者,至少不是您期望的那样.您可以创建自己的视图类或mixin,以使用两种形式,但AFAIK Django不提供这种形式.
Nope, you can't use built-in class based views for this. Or, at least, not on the high-level you'd expect. You can make your own view class or mixin that will work with two forms, but AFAIK Django doesn't provide one.
这篇关于在Django 1.5中以一种形式使用两个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!