Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8577349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:08:22+00:00 2026-06-11T20:08:22+00:00

I have 2 models, Father and Son . I have a page to register

  • 0

I have 2 models, Father and Son.

I have a page to register Father. On the same page I have a formset to register Son.

On page has a button “more” to add another Father and their respective Son on the same page.

Does anyone have any examples using CreateView?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:08:23+00:00Added an answer on June 11, 2026 at 8:08 pm

    Class based views are still new, so I’ll write this out. The process is simple:

    First, create the forms for your objects. One of the forms will be repeated. Nothing special to be done here.

    class SonInline(ModelForm):
        model = Son
    
    class FatherForm(ModelForm):
        model = Father
    

    Then, create your formset:

    FatherInlineFormSet = inlineformset_factory(Father,
        Son,
        form=SonInline,
        extra=1,
        can_delete=False,
        can_order=False
    )
    

    Now, to integrate it with your CreateView:

    class CreateFatherView(CreateView):
        template_name = 'father_create.html'
        model = Father
        form_class = FatherForm # the parent object's form
    
        # On successful form submission
        def get_success_url(self):
            return reverse('father-created')
    
        # Validate forms
        def form_valid(self, form):
            ctx = self.get_context_data()
            inlines = ctx['inlines']
            if inlines.is_valid() and form.is_valid():
                self.object = form.save() # saves Father and Children
                return redirect(self.get_success_url())
            else:
                return self.render_to_response(self.get_context_data(form=form))
    
        def form_invalid(self, form):
            return self.render_to_response(self.get_context_data(form=form))
    
        # We populate the context with the forms. Here I'm sending
        # the inline forms in `inlines`
        def get_context_data(self, **kwargs):
            ctx = super(CreateFatherView, self).get_context_data(**kwargs)
            if self.request.POST:
                ctx['form'] = FatherForm(self.request.POST)
                ctx['inlines'] = FatherInlineFormSet(self.request.POST)
            else:
                ctx['form'] = Father()
                ctx['inlines'] = FatherInlineFormSet()
            return ctx
    

    Finally, here is the template:

    The key part is the jquery django-dynamic-formset plugin that keeps adding new inline forms:

    <form id="father-form" method="POST" enctype="multipart/form-data" action=".">
    {% csrf_token %}
    <div class="row">
      {% for f in form %}
        <div class="span3">{{ f.label }}<br />{{ f }}
          {% if f.errors %}
              {% for v in f.errors %}
                <br /><span style="color:red;">{{ v }}</span>
              {% endfor %}
          {% endif %}
        </div>
     {% endfor %}
    </div>
    <hr />
    <h2>Sons:</h2>
    <table class="table-striped">
     <table>
     {%  for f2 in inlines %}
       <tr id="{{ f2.prefix }}-row">
          {% for i in f2 %}
            <td>
               {{ i }}{% if i.errors %}<span style="color:red;">{{ i.errors }}</span>{% endif %}
            </td>
          {% endfor %}
       </tr>
     {% endfor %}
    </table>
    {{ inlines.management_form }}
    <input type="submit" class="btn btn-primary" value="Go Go Gadget &rarr;">
    </form>
    <script type="text/javascript">
        $(function() {
            $('#father-form tr').formset({
                prefix: '{{ inlines.prefix }}'
            });
        })
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have models Page and Commit. Page has many Commits. But sometimes i need
I have two Models, Programme and Event, a programme has many events. I need
I have models that belong to some 'group' (Company class). I want to add
I have models Foo and Bar . Bar has column foo_id . When I
I have models foo, bar, and baz like so # has a field join_condition_string
I have models in project that use more than one table to select. How
I have models corresponding to database tables. For example, the House class has color,
I have models and their admin code at below. The question is how can
I have models like this: class Vendor(models.Model): title = models.CharField() class Product(models.Model): ... vendor
I have models Product and Category . Category can have many products. ( Product

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.