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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:43:35+00:00 2026-06-08T05:43:35+00:00

This is probably a fairly simple question, but I can’t seem to figure it

  • 0

This is probably a fairly simple question, but I can’t seem to figure it out from the Django Docs. I’m trying to save a two ModelForms at once with one being a ForeignKey of another. I’m not sure how to write the logic in the views to ensure these go together properly.

models.py

class Address(models.Model):
    address = models.CharField(max_length=100)
    city = models.CharField(max_length=50)
    zipcode = models.PositiveIntegerField()

class Store(models.Model):
    name = models.CharField(max_length=100)
    description = models.CharField(max_length=140, blank=True)
    address = models.ForeignKey(Address, null=True)

forms.py

class CreateStore1Form(forms.ModelForm):
    class Meta:
        model = Store
        exclude = ('address',)

class CreateStore2Form(forms.ModelForm):
    class Meta:
        model = Address

views.py

@login_required
def create(request):
    if request.method == "POST":
        form1 = CreateStore1Form(request.POST)
        form2 = CreateStore2Form(request.POST)
        if form1.is_valid() and form2.is_valid():
            store = form1.save(address)
            new_address = form2.save(commit=False)
            new_address.store = store
            mew_address.save()
    else:
        form1 = CreateStore1Form()
        form2 = CreateStore2Form()
    return render(request, 'create.html', locals())

Any help would be appreciated. Thanks!

  • 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-08T05:43:36+00:00Added an answer on June 8, 2026 at 5:43 am

    Django provides inline formsets for this use case:

    Inline formsets is a small abstraction layer on top of model formsets.
    These simplify the case of working with related objects via a foreign
    key.

    forms.py

    class AddressForm(forms.ModelForm):
        class Meta:
            model = Address
    
    class StoreForm(forms.ModelForm):
        class Meta:
            model = Store
            exclude = ('address',)
    

    views.py

    from django.forms.models import inlineformset_factory
    
    @login_required
    def create(request):    
        AddressInlineFormSet = inlineformset_factory(Address, Store, form=AddressForm)
    
         if request.method == 'POST':
             storeForm = StoreForm(request.POST)
    
             if storeForm.is_valid():
                 new_store = storeForm.save()
                 addressInlineFormSet = AddressInlineFormSet(request.POST, request.FILES, instance=new_store)
    
                 if addressInlineFormSet.is_valid():
                    addressInlineFormSet.save()
                    return HttpResponseRedirect(reverse('some_happy_customer_url'))
    
                 else:
                    classificationformset = ClassificationInlineFormSet(request.POST, request.FILES, instance=new_store)
         else:
              addressInlineFormSet = AddressInlineFormSet()
              storeForm = StoreForm()
         return render(request, 'create.html', locals())
    

    Please see also this question for more details.

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

Sidebar

Related Questions

Probably a fairly simple question but I can't seem to make sense of why
I think this question is probably fairly simple, but I've been searching around and
Hey guys, sorry for this probably fairly simple question but I'm starting to get
Hi all I have a (probably) simple problem but I just can't figure out
I'm not sure if this possible but probably is fairly simple. I've made a
I feel like the answer to this question is probably very simple, but I'm
I'm fairly sure this is a template question, since I can't seem to solve
This is probably a fairly simple way to do it, but I ask since
Guys, this probably will be fairly simple, but how am I able to find
There probably is a fairly simple and straight-forward answer for this, but for some

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.