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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:20:55+00:00 2026-05-29T05:20:55+00:00

Below is simplified example of what I am trying to achieve: class Product(models.Model): #

  • 0

Below is simplified example of what I am trying to achieve:

class Product(models.Model):
    # some data, does not really matter

class ProductAttributeValue(models.Model):
    product = models.ForeignKey('Product')

    value=models.CharField(_("value"),max_length=100)

….

class ProductForm(forms.ModelForm):

    def __init__(self,*args, **kwargs):
        super(ProductForm, self).__init__(*args, **kwargs)

        #Here, I am dynamically constructing and injecting attributes.
        #my products have dynamic attributes
        # the filled-in values of these attributes need to be saved as ProductAttributeValue instances
        #...


    def save(commit):
        m = super(ProductForm, self).save(commit=False)

        #looping thru my custom attributes and constructing instances
        #to simplify I will just put one example
        attr_val=ProductAttributeValue(product=m)
        attr_val.value=self.clean_data['myval']
        m.productattributevalue_set.add(attr_val)

        if commit:
            m.save()

        # also doing m2m_save if exists

        return m

So as I was expecting this fails with product_id=None error. I also tried to understand how django’s InlineForm (on admin side) work, but seems they are saving the main Product first and then the ProductAttributeValue, and if say ProductAttributeValue saving fails they are fine.
For my case it’s not acceptable, i.e. either I should save all the form (both product and the value) or fail. I can certainly save with commit=True from the beginning but as I said, I don’t want a case where product is saved and value not.

Any help appreciated.

  • 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-29T05:20:56+00:00Added an answer on May 29, 2026 at 5:20 am

    See: https://docs.djangoproject.com/en/dev/topics/db/transactions/
    I think you can do this using a manual transaction. Commit the first save and then rollback if the second fails:

    @transaction.commit_manually
    def viewfunc(request):
        ...
        # You can commit/rollback however and whenever you want
        transaction.commit()
        ...
    
        # But you've got to remember to do it yourself!
        try:
           ...
        except:
           transaction.rollback()
        else:
           transaction.commit()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to parse some HTML with XPath. Following the simplified XML example below,
I coded some calculation stuff (I copied below a really simplifed example of what
I've simplified my JavaScript code to the example below; this code is giving me
I’m using Linq to extract values from some XML. Shown below is a simplified
Given the example queries below (Simplified examples only) DECLARE @DT int; SET @DT=20110717; --
Below is a simplified example of a stored procedure I've created. DELIMITER // CREATE
I have given below simplified JavaScript problem. var PROJ=(function(){ var tags={}, var lock=true; function
Below is my (simplified) schema (in MySQL ver. 5.0.51b) and my strategy for updating
The code below is a simplified version of my website. On my site, the
I have below a working query that needs to be simplified. The reason is

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.