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

  • Home
  • SEARCH
  • 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 8587127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:29:28+00:00 2026-06-11T22:29:28+00:00

I have run into a chicken and egg situation. I have a formset where

  • 0

I have run into a chicken and egg situation.
I have a formset where the user can modify existing data. The instance shall not be saved directly though but instead it shall be added as a new object.

for fm in attached_deals_formset:                
    if fm.has_changed():
        modified_deal = fm.save(commit=False)
        deal = Deal.objects.create(deal_id = modified_deal.deal_id, ... )
        for item in modified_deal.sales_item: #m2m
            deal.sales_item.add(item)
        deal.save_m2m();

My problem is sales_item which is a m2m field. Just by traversing through it it crashes:

for item in modified_deal.sales_item:

‘Deal’ instance needs to have a primary key value before a
many-to-many relationship can be used.

I don’t understand though, these are the multi-selects that the user has selected within modified_deal, why does it throw an exception by just traversing through it? And how could I solve this?

  • 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-11T22:29:30+00:00Added an answer on June 11, 2026 at 10:29 pm

    I found the solution. modified_deal = fm.save(commit=False) only contains the class values. The m2m fields point to separate tables and hence are not set yet.

    In order to get access to the m2m selection of the user, simply use that field as part of the form.cleaned_data[] rather part of the model:

        modified_deal = fm.save(commit=False)
        deal = Deal.objects.create(deal_id = modified_deal.deal_id, ... )
        for item in fm.cleaned_data['sales_item']:
             deal.sales_item.add(item)
        deal.save();
    

    This works for me.

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

Sidebar

Related Questions

I have run into a situation where there is 'bad' data in a number
I have run into a situation where I want to ensure that a compound
I have run into a problem trying to modify a form I myself have
I have run into a very strange behaviour I can’t make sense of. I
I have run into the following error trying to create a user in django:
I have run into a problem with generics in Java and I can't find
I have run into a situation where I need to compare two different lists
I have run into a bit of a problem, well not sure if it
I have run into an issue regarding how to identify which user owns particular
I have run into a common, yet difficult problem. I do not use Photoshop

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.