Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django form with fields from two different models

I need to display one form, with multiple fields from 2 different models. Form will contain only part of fields from models, and layout will be made using the crispy forms.

My models:

class Company(BaseModel):
    title = models.CharField(_('Company'), max_length=128)
    domain = models.CharField(_('Domain'), max_length=128)
class Account(BaseModel):
    company = models.ForeignKey(Company)
    user = models.OneToOneField(User)
    role = models.CharField(_('Role'), choices=ROLES, default='member', max_length=32)

Fields which I want to show in form: company title, user first name, user last name, user email

Is it even possible? How can I do this?

like image 554
dease Avatar asked Jan 15 '15 16:01

dease


People also ask

How can I have multiple models in a single Django ModelForm?

You can just show both forms in the template inside of one <form> html element. Then just process the forms separately in the view. You'll still be able to use form.

How do you exclude a specific field from a ModelForm?

Set the exclude attribute of the ModelForm 's inner Meta class to a list of fields to be excluded from the form.


2 Answers

The other answers on this page involve tossing away the benefits of model forms and possibly needing to duplicate some of the functionality you get for free.

The real key is to remember that one html form != one django form. You can have multiple forms wrapped in a single html form tag.

So you can just create two model forms and render them both in your template. Django will handle working out which POST parameters belong to each unless some field names clash - in which case give each form a unique prefix when you instantiate it.

Forms:

class CompanyForm(forms.ModelForm):
    class Meta:
        fields = [...]
        model = Company

class AccountForm(forms.ModelForm):
    class Meta:
        fields = [...]
        model = Account

View:

if request.method == 'POST':

    company_form = CompanyForm(request.POST)
    account_form = AccountForm(request.POST)

    if company_form.is_valid() and account_form.is_valid():

        company_form.save()
        account_form.save()
        return HttpResponseRedirect('/success')        

    else:
        context = {
            'company_form': company_form,
            'account_form': account_form,
        }

else:
    context = {
        'company_form': CompanyForm(),
        'account_form': AccountForm(),
    }

return TemplateResponse(request, 'your_template.html', context)

Template:

<form action="." method="POST">
    {% csrf_token %}
    {{ company_form.as_p }}
    {{ account_form.as_p }}
    <button type="submit">
</form>
like image 187
Andy Baker Avatar answered Sep 28 '22 18:09

Andy Baker


In your forms.py

from django import forms


class YourForm(forms.Form):
    title = forms.CharField()
    first_name = forms.CharField()
    last_name = ...

In your views.py

from forms import YourForm
from django import views
from models import Company, Account

class YourFormView(views.FormView)
    template_name = 'some_template.html'
    form_class = YourForm
    success_url = '/thanks/'

    def form_valid(self, form):
        title = form.cleaned_data['title']
        ...
        # do your processing here using Company and Account
        # i.e. company = Company.objects.create(title=title, ...)
        #      account = Account.objects.get_or_create(
        #      user=..., company=company ...)
        #      ... more processing
        #
        # Call company.save() and account.save() after adding
        # your processed details to the relevant instances
        #  and return a HttpResponseRedirect(self.success_url)

    def is_valid(self):
        # don't forget to validate your fields if need be here

As usual the docs are pretty helpful. https://docs.djangoproject.com/en/1.7/topics/forms/

like image 20
zsoobhan Avatar answered Sep 28 '22 18:09

zsoobhan