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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:13:08+00:00 2026-06-06T15:13:08+00:00

I have a formset in django that uses a model with a one to

  • 0

I have a formset in django that uses a model with a one to many relationship. Many attendees can attend a single event.

I am sending the ‘many’ part of the model in a formset. The problem is that the data is returned with no primary key set. I am getting a validation error because the formset primary key data is not set.

Back in my view, I know what the primary key is supposed to be. And i want to set it before validation. Either that or set the value before I send out the form for display. But I don’t know how to set it in the formset data.

In the code below, foreign key is event.

Option 1: Set the foreign key before showing the form:

#models.py  - note 2 foreign keys
class Attendee(models.Model):
    event = models.ForeignKey(Event)
    sponsor = models.ForeignKey(Customer)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30) 

# forms.py
class AttendeeForm(forms.ModelForm): 
  event = forms.IntegerField(required=True, widget=forms.HiddenInput()) 

  class Meta: 
    model = Attendee 
    fields = ( 'last_name', 'first_name', ) 
    exclude = ('event', )  # excluded so user can't set foreign key

# views.py
event = get_object_or_404(Event, id=event_id)
AttendeeFormSet = inlineformset_factory(Event, Attendee, form=AttendeeForm, extra=1)
formset = AttendeeFormSet(instance=event, prefix='attendees')
# Error on form submit:  
#  Returns and redisplays the form along with message.
#  (Hidden field event) The inline foreign key did not match the parent instance primary key.


#  I also tried:  
#AttendeeFormSet = inlineformset_factory(Event, Customer, Attendee, form=AttendeeForm, extra=1)
# which gave error: 
# inlineformset_factory() got multiple values for keyword argument 'form'

Option 2: Set the foreign key on the return POST. Note even though data is present, the form is not valid because foreign key is not set.

formset = AttendeeFormSet(request.POST, request.FILES, prefix='attendees')
i=0
for form in formset.forms:
   # How can I set the foreign key (event) field here?
   pdb.set_trace()

if formset.is_valid():
   attendees = formset.save_all()


(Pdb) p form.fields
{'last_name': <django.forms.fields.CharField object at 0xaa44a2c>, 
'first_name':    <django.forms.fields.CharField object at 0xaa449ec>,  
'event': <django.forms.fields.IntegerField object at 0xaa44fec>}
(Pdb) 

What is the best way to fix my foreign key problem in my formset? If Option 2 How do I set this field before I test is_valid?

  • 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-06T15:13:09+00:00Added an answer on June 6, 2026 at 3:13 pm

    Since this is a formset representing a foreign key relationship, you should use inlineformset_factory to construct the class. That will take care of setting the FKs for you:

    AttendeeFormSet = formset_factory(Event, Attendee, form=AttendeeForm)
    ...
    formset = AttendeeFormSet(instance=my_event)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an edit object view that contains a formset(one or many if this
I have a Model Formset that should take files sent via POST and add
I've looked at formset and model formset at Django many times, but I still
Say I have django model that looks something like this: class Order(models.Model): number =
I have a ManyToMany field with a relationship model. I want a formset, filtered
I have created a formset from a model that has an ImageField inside it.
My problem is similar to Django Passing Custom Form Parameters to Formset Ive have
Is it possible in django to have a ChoiceField on a formset level rather
Have one Doubt In MVC Architecture we can able to pass data from Controller
Thanks to the fantastic inline model formsets in django I have a pretty advanced

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.