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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:06:53+00:00 2026-06-16T00:06:53+00:00

Say I have two basic classes, one with a ManyToMany relationship to the other:

  • 0

Say I have two basic classes, one with a ManyToMany relationship to the other:

class Foo(models.Model):
    abc = models.TextField()
    test = models.ManyToManyField(Bar)

class Bar(models.Model):
    zyx = models.TextField()

And then I create a Django form for Foo:

class FooForm(forms.ModelForm):
    class Meta:
        model = Foo
        fields = ["abc", "test"]

This will create a form with a TextArea for me to input the value of the new Foo‘s abc variable, as well as a listbox to link it to one or more Bar objects. What I would like to do for this particular form, is instead of the listbox for the Bar objects, be able to create one (and only one) new Bar at the same time (i.e., display a second TextArea for the value of Bar‘s zyx) and then link that new Bar to the new Foo. The ManyToMany relationship comes in later on in editing, but at creation, I want exactly one Bar tied to the one new Foo.

Is it possible to do this all at once by POSTing the form, or am I going to have to use some javascript and AJAX to actually create the Bar first, get its new ID, and link the new Foo to that?

  • 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-16T00:06:54+00:00Added an answer on June 16, 2026 at 12:06 am

    It is possible, but I’m not sure a ModelForm would do that for you.


    However, you can easily do it on your own:

    Set up the forms for the user to fill-in:

    • Create a form to let the user input abc (that could be a ModelForm)
    • Create a formset out of a form that let the user enter an arbitrary number of zyx to create related Bar instances. (Again, formset of ModelForm is OK)

    When processing the POST data, do the following:

    1. Validate the form and the formset are valid
    2. Insert the new instances
    3. Add your new Bar instances to your new Foo‘s Bar ManyToMany relationship

    Small tip: remember that ModelForm.save returns the instance!

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

Sidebar

Related Questions

Say I have two separate classes, A and B. I also have Repository class
say i have two collections in mongodb,one for users which contains users basic info,and
I've got a basic question about obj-c coding, say I have two classes represented
Let's say that I have two classes A and B. class A { private:
Let's say I have these two very basic entities: public class ParentEntity { public
Say I have two classes and have a requirement that the primary key property
Say you have two classes, A and B. Is it possible to instantiate both
Let's say we have two tables, user and domain . User may have one
Assume you have two entities, one Article and the other one is Fruits .
Let say I have a basic 2D vector class something like class vector2 {

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.