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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:11:11+00:00 2026-06-02T16:11:11+00:00

If I create a formset using modelformset_factory like this: IngredientFormSet = modelformset_factory(RecipeIngredients, form=RecipeIngredientsForm) formset

  • 0

If I create a formset using modelformset_factory like this:

 IngredientFormSet = modelformset_factory(RecipeIngredients, form=RecipeIngredientsForm)
 formset = IngredientFormSet(request.POST)

and my form looks like this

class RecipeIngredientsForm(forms.ModelForm):
    Ingredient = forms.CharField(max_length= 100)
    class Meta:
        model = RecipeIngredients
        exclude = ('weightmetric','recipe')

Where would I put my custom .save() method? Would I put it under the RecipeIngredientsForm?

[a potential solution]

In your view do something like this:

 if formset.is_valid():
        for form in formset:
            obj = form.save(commit=False) #obj = RecipeIngredient model object
            try:
                ingredient_in_db = Ingredient.objects.get(name = form.cleaned_data.get('ingredientform'))
            except:
                ingredient_in_db = None
            if ingredient_in_db:
                obj.ingredient = ingredient_in_db
            else:
                new_ingredient = Ingredient.objects.create(name = form.cleaned_data.get('ingredientform'))
                obj.ingredient = new_ingredient
            obj.recipe = recipeobj
            obj.save()

Incidentally, I think this method would also allow me to do a custom .save(), given that I take each form in the formset and do a form.save(commit= False) on it. It was easier though, to just do it in my view, since I needed access to the recipe object.

  • 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-02T16:11:13+00:00Added an answer on June 2, 2026 at 4:11 pm

    I’m not sure of the best way to override the existing save() method on the BaseModelForm. My guess is that you actually would want to either:

    1. Create your own field that has it’s own to_python() method which will take the text and find/create the associated object.

      class IngredientField(forms.CharField):
          def to_python(self, value):
              # Lookup model or create new one
              ingredient = models.Ingredient.objects.get(name=value)
              if not ingredient:
                  ingredient = models.Ingredient.create(name=value)
              return ingredient
      
    2. Use save(commit=False) on the formset, do the ingredient lookup/creation, and then commit afterwards

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

Sidebar

Related Questions

How can I update an object from a formset using request.POST ? Here is
Say I have django model that looks something like this: class Order(models.Model): number =
My model class Member(models.Model): dob = models.DateField('dob') form class MemberForm(ModelForm): dob = forms.DateField(required=False, input_formats='%Y-%m-%d')
I've created a simple contact form using the modelformset_factory to build the form in
I'm using a piece of JQuery to create the effect described at this website:
This is my views.py: # Create your views here. def codepost(request): if request.method ==
I'm using an inline formset with some javascript to add more forms with a
I am using Django ModelForms to create a form. I have my form set
I'm attempting to create a formset for the following models: class Category(models.Model): name =
Create facebook app Using a cURL to post a message from an App but

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.