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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:23:26+00:00 2026-05-16T22:23:26+00:00

I need help on how to save the same (reference to an) object into

  • 0

I need help on how to save the same (reference to an) object into a ManyToManyField.
For example I have models like this:

class Material(models.Model):
    name = models.CharField(max_length=50)

class Compound(models.Model):
    materials = models.ManyToManyField(Material)

In this example, the Compound can be made of one or many different Materials, and it also could be made from the same Material twice (same id in Material model).

If I try to save through a ModelForm, the second Material is discarded because it has the same id as the first Material.

What is the best approach for this?

Thank you!

  • 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-05-16T22:23:26+00:00Added an answer on May 16, 2026 at 10:23 pm

    I would suggest doing this as per http://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-manytomany

    class Material(models.Model):
        name = models.CharField(max_length=50)
    
    class Compound(models.Model):
        materials = models.ManyToManyField(Material, through='CompoundMaterials')
    
    class CompoundMaterials(models.Model)
        Material = models.ForeignKey(Material)
        Compound = models.ForeignKey(Compound)
        Quantity = models.IntegerField()
    

    What am I doing here? Well, Django normally automatically generates an intermediary table for holding pairs of keys associating compounds to elements. In this case we’re definiting it ourselves, but not only that, we’re adding additional data to the relationship i.e. the quantity you speak of.

    As an example usage, what you might do is this:

    $ python manage.py shell
    from project.app.models import *
    
    oxygen = Material(name="oxygen")
    hydrogen = Material(name="hydrogen")
    water = Compound(name="water")
    
    oxygen.save()
    hydrogen.save()
    water.save()
    
    water_chemistry_oxygen = CompoundMaterials(Material=oxygen, Compound=Water, Quantity=1)
    water_chemistry_hydrogen = CompoundMaterials(Material=hydrogen, Compound=Water, Quantity=2)
    
    water_chemistry_oxygen.save()
    water_chemistry_hydrogen.save()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

need help in error in database pivot. i have table tamed table_score like below:
Hello i have the this problem, i need your help to figure out what
Okay, I need some help here. This is the same old "can't use an
I really need help with this one. I have written an adding that adds
Need help with a query that I wrote: I have three tables Company id
Need help writing a script downloads data from google insight using c# this is
need help/guide for sql select query, I have 2 table stock and stock_history, in
need help regarding USSD Gateway. I have to develop an app, which will directly
I need help with one ajax function This is raw page setup. Page will
I need help...I don't have much hair left to pull out. I use Eclipse

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.