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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:23:48+00:00 2026-05-28T14:23:48+00:00

During form processing I’d like to be able to set a foreign key field

  • 0

During form processing I’d like to be able to set a foreign key field on a model object without the user having to select the key from a dropdown.

For instance:

#models.py
class AAA(models.Model):

    some_field = models.TextField()

class BBB(models.Model):
    another_field = models.TextField()
    key_field = models.ForeignKey('AAA')

The user will navigate from a view showing an instance of ‘AAA’ to a create_object style view that will create an instance of ‘BBB’ given a parameter referring to ‘AAA’. The foreign key is set in code to point back to the ‘AAA’ instance.

The django comments framework seems to do this but I can’t figure out how.

Any ideas? I’m sure it should be quite simple.

  • 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-28T14:23:49+00:00Added an answer on May 28, 2026 at 2:23 pm

    You can exclude the key_field from your model form, save with commit=False, then set key_field in your view before saving to the database.

    class BBBForm(forms.ModelForm):
        class Meta:
            model = BBB
            exclude = ("key_field",)
    
    def create_view(request, **kwargs):
        if request.method == "POST":
            aaa = # get aaa from url, session or somewhere else
            form = BBBForm(request.POST)
            if form.is_valid():
                bbb = form.save(commit=False)
                bbb.key_field = aaa
                bbb.save()
                return HttpResponseRedirect("/success-url/")
            ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So the chain of events is: The user submits a form. During the processing
I trying to understand if a isset is required during form processing when i
I have a web-application. The flow of processing a form in it goes like
I want to check during edit form saving process the value of a field
During my work on this database application, I've apparently managed to corrupt a form
I am changing the Visibility of a Form to false during the load event
Is it possible to redirect to different location in controller during ajax processing? I
During a form submission, I have to verify something and I don't want to
I am unable to determine which form submit button is clicked during an Ajax
I am using form authentication inmy ASP.NET 2.0 website. Today during testing i was

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.