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 8838421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:59:26+00:00 2026-06-14T09:59:26+00:00

Here is how my models are setup: class ModelA(models.Model): user = models.ForeignKey(User) modelB =

  • 0

Here is how my models are setup:

class ModelA(models.Model):
    user = models.ForeignKey(User)
    modelB = models.ForeignKey('ModelB', related_name='modelB')
    # more fields here

class ModelB(models.Model):
    user = models.ForeignKey(User)
    # more fields here

The user field in both classes are just so that both of these can be queried by the same user later on.

I have a ModelForm of ModelB and I would like to have the users be able to fill out a ModelB ModelForm with a dynamic number of ModelA instances. I would like ModelB’s form to start out with 1 instance of ModelA to fill out and then the user should be able to add more ModelA’s to ModelB’s form.

I’m stuck on this as a personal project and would really like to move forward. Thanks for any help!

Edit 1: I know that I will most likely need to do some work by overloading ModelForms def __init__, but that is about as far as I have gotten. Also I am using ClassBasedGeneric Views (CreateView to get the add working and an EditView later on), but I am willing to move away from that if needed.

  • 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-14T09:59:27+00:00Added an answer on June 14, 2026 at 9:59 am

    You require django inline formset

    In view:

    f_set = inlineformset_factory(ModelB, ModelA, extra=1)
    

    In template lets suppose you are rendering form_set in a div whose id is one_to_many:

    <form>
       <div id="one_to_many">
           {{f_set.as_table}}
       </div>
    </form>
    
    #This will render some thing like this
    <form>
        <div id="one_to_many">
            <table>
                <tr>
                   <th>
                       <label for="id_form-0-field_name">Field Name:</label>
                   </th>
                   <td>
                      <input id="id_form-0-field_name" type="text" name="form-0-field_name" value="" maxlength="100" />
                      <input type="hidden" name="form-0-id" value="1" id="id_form-0-id" />  
                    </td>
                </tr>
            </table>
            <input type="button" value="Add More Field" id="add_more">
        </div>
    </form>
    

    Now the jquery part to add field dynamically:

    $(document).ready(function(){
            $('#add_more').click(function(){
                var tbl = $('#one_to_many').children('table');
                var last_id = parseInt(tbl.find('input[type=hidden]').last().val());
                var next_id = last_id + 1;
                htm = '<tr><th><label for="id_form-'+last_id+'-field_name">Field Name:</label></th><td><input id="id_form-'+last_id+'-field_name" type="text" name="form-'+last_id+'-field_name" value="" maxlength="100" /><input type="hidden" name="form-'+last_id+'-id" value="'+next_id+'" id="id_form-'+last_id+'-id" /></td></tr>'
                tbl.find('tr').last().after(htm);
    
            });    
        });
    

    You can test this on jsfiddle. Note this will only work if you render your form as_table

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here is my model class gameUserTrophyInfo(models.Model): user = models.ForeignKey(userInfo) trophy = models.ForeignKey(gameTrophyInfo) date =
Here's a snippet from my application: class PortolioItem(models.Model): ... user = models.ForeignKey(User) contract =
First here's my current setup: models/user.rb class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable,
Okay so I have this mode: class Posts(db.Model): rand1 = db.FloatProperty() #other models here
Here is my models, class Age(models.Model): layer = models.CharField(max_length=50) order = models.PositiveIntegerField() ... class
Here's part of my Django app's models.py : class Person(models.Model): birth_year = WideYear(null=True, blank=True)
Here is a simplified version of one of my models: class ImportRule(models.Model): feed =
I have my models & associations currently setup like so: class User < ActiveRecord::Base
I have the following model setup. from django.db import models from django.contrib.auth.models import User
Here are my models: **Resource** has_many :users, :through => :kits has_many :kits **User** has_many

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.