Jacob Valenta
Jacob Valenta

Reputation: 6769

Django: Create two models with a CreateView

I have a CreateView for creating a customer, but I also need to create an 'identification' model along with this customer. I have an identification model that has a foreign key to the model because we need to be able to add any amount of IDs to some (Drivers license, Passport, etc)

Anyways, the current code (Which only creates a new customer) looks like this:

class CustomerCreationView(CreateView):
    template_name = "customers/customer_information.html"
    form_class = CustomerInformationForm

    def get_context_data(self, *args, **kwargs):
        context_data = super(CustomerCreationView, self).get_context_data(*args, **kwargs)

        context_data.update({
            'new_customer': True,
        })

        return context_data

CustomerInformationForm is ModelForm. I would like to create another ModelForm for Identifications, but I do not know how to add the second form to a CreateView. I found this article, but it is 5 years old and not talking about a CreateView.

Upvotes: 0

Views: 4434

Answers (3)

BRIJESH
BRIJESH

Reputation: 11

you can solve this problem using this approach.

first step pass another form as a context in a template.

second step override a post method.

class CustomerCreationView(CreateView):
    template_name = "customers/customer_information.html"
    form_class = CustomerInformationForm
   
    def get_context_data(self, *args, **kwargs):
        context_data = super(CustomerCreationView, self).get_context_data(*args, **kwargs)
        context_data["yournewform"] = YourNewForm(self.request.POST or None, self.request.FILES or None)
        return context_data

    def post(self, request, *args, **kwargs):
        form = self.get_form()
        yournewform = YourNewForm(request.POST, request.FILES)
        if form.is_valid() and yournewform.is_valid():
            '''
            # this is just example, you can change according to your model and form
            user = YourNewForm.save(commit=False)
            user.set_password(user.password)
            user.roll = 'parent'
            user.save()
            parent = form.save(commit=False)
            parent.user = user
            parent.save()
            messages.success(request, "Parent Created successfully")
            return redirect("parents")
            '''
        else:
            return render(request, self.template_name, {"form":form, "yournewform":yournewform})

Upvotes: 0

Alejandro
Alejandro

Reputation: 551

class CustomerCreationView(CreateView):
    template_name = "customers/customer_information.html"
    form_class = CustomerInformationForm
    other_form_class = YourOtherForm

    def get_context_data(self, *args, **kwargs):
        context_data = super(CustomerCreationView, self).get_context_data(*args, **kwargs)

        context_data.update({
            'new_customer': True,
            'other_form': other_form_class,    
        })

        return context_data

Upvotes: 0

Anton Khamets
Anton Khamets

Reputation: 159

You could use CreateWithInlinesView from django-extra-views. The code would look like this:

from extra_views import CreateWithInlinesView, InlineFormSet


class IdentificationInline(InlineFormSet):
    model = Identification


class CustomerCreationView(CreateWithInlinesView):
    model = CustomerInformation
    inlines = [IdentificationInline]

Upvotes: 8

Related Questions