get field from anther model in model form

Solutions on MaxInterview for get field from anther model in model form by the best coders in the world

showing results for - "get field from anther model in model form"
Paulina
15 Oct 2017
1if request.method == 'POST':
2
3    company_form = CompanyForm(request.POST)
4    account_form = AccountForm(request.POST)
5
6    if company_form.is_valid() and account_form.is_valid():
7
8        company_form.save()
9        account_form.save()
10        return HttpResponseRedirect('/success')        
11
12    else:
13        context = {
14            'company_form': company_form,
15            'account_form': account_form,
16        }
17
18else:
19    context = {
20        'company_form': CompanyForm(),
21        'account_form': AccountForm(),
22    }
23
24return TemplateResponse(request, 'your_template.html', context)
25
Leonardo
28 Oct 2016
1<form action="." method="POST">
2    {% csrf_token %}
3    {{ company_form.as_p }}
4    {{ account_form.as_p }}
5    <button type="submit">
6</form>
7
Jessica
04 May 2019
1class CompanyForm(forms.ModelForm):
2    class Meta:
3        fields = [...]
4        model = Company
5
6class AccountForm(forms.ModelForm):
7    class Meta:
8        fields = [...]
9        model = Account
10