简体   繁体   中英

Using two models in one form in django 1.5

How can I use two models with OneToOne relation in one form using the CreateView in Django 1.5?

My models are these:

class Act(models.Model):
    name = models.CharField()

class DetailAct(models.Model):
    detail = models.CharField()
    act = models.OneToOneField(Act)

My forms

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

Thank you

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM