简体   繁体   English

Django-如何使用相同形式的多个ModelForm?

[英]Django - How to work with multiple ModelForm's in the same form?

I've 4 models that I need to store data at once. 我有4个模型需要一次存储数据。 For that I'm thinking using ModelForms. 为此,我正在考虑使用ModelForms。

I've tested with 2 ModelForm's at the same time but it is not working. 我同时测试了2个ModelForm,但无法正常工作。 Here is the code. 这是代码。

Model: 模型:

class Main(models.Model):
    section             = models.ForeignKey(Section)
    user                = models.ForeignKey(User)
    title               = models.CharField(max_length=250)
    date_inserted       = models.DateTimeField(auto_now_add=True)
    date_last_update    = models.DateTimeField(auto_now=True)

    def __unicode__(self):
    return self.title

    # To order in the admin by name of the section
    class Meta:
    ordering = ['date_inserted']


class BicycleAd(models.Model):
    main                = models.ForeignKey(Main)
    bicycleadtype       = models.ForeignKey(BicycleAdType)
    bicycleaditemkind   = models.ForeignKey(BicycleAdItemKind) # MPTT Model
    bicycleadcondition  = models.ForeignKey(BicycleAdCondition)
    country             = models.ForeignKey(GeonamesCountry)       
    city                = models.ForeignKey(GeonamesLocal) 
    date_inserted       = models.DateTimeField(auto_now_add=True)
    date_last_update    = models.DateTimeField(auto_now=True)

    # To order in the admin by name of the section
    class Meta:
    ordering = ['date_inserted']   

Forms: 形式:

class MainForm(forms.ModelForm):
    class Meta:
    model = Main
    exclude = ('user', 'section')

class BicycleAdForm(forms.ModelForm):
    class Meta:
    model = BicycleAd
    exclude = ('main', 'bicycleadtype', 'bicycleaditemkind', 'bicycleadcondition', 'city')

View: 视图:

def submit_data_entry_view(request):
    form_main      = MainForm(request.POST)
    form_bicyclead = BicycleAdForm(request.POST)

    return render_to_response('app/submit_data_entry.html', {'form_main': form_main, 'form_bicyclead': form_bicyclead}, context_instance=RequestContext(request))

Template: 模板:

<form method="post" action="">
    {{form_main}}
    {{form_bicyclead}}
</form>

At the end I only get the"form_bicyclead" outputed in the browser? 最后我只能在浏览器中输出“ form_bicyclead”吗? How can I get the two forms at once? 我如何一次获得两种表格?

Best Regards, 最好的祝福,

are you using submit_data_entry_view to render forms too? 您是否也在使用submit_data_entry_view呈现表单? shouldn't it be like - 不应该是这样-

def submit_data_entry_view(request):

    if request.method == 'POST': #form submit
        form_main      = MainForm(request.POST)
        form_bicyclead = BicycleAdForm(request.POST)

        #now process and save the form

        return <whatever_you_want>
    elif request.method == 'GET': #first time rendering the form
        form_main      = MainForm()
        form_bicyclead = BicycleAdForm()

        return render_to_response('app/submit_data_entry.html', {'form_main': form_main, 'form_bicyclead': form_bicyclead}, context_instance=RequestContext(request))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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