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

  • Home
  • SEARCH
  • 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 982143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:39:03+00:00 2026-05-16T04:39:03+00:00

How do i pre-populate the exclude fields in a ModelFormSet. AuthorFormSet below doesn’t take

  • 0

How do i pre-populate the exclude fields in a ModelFormSet. AuthorFormSet below doesn’t take an instance argument here, only queryset. So how do i force the empty forms coming in not to have a NULL/None user attribute. I want to be able to set that user field to request.user

models.py

class Author(models.Model):
    user = models.ForeignKey(User, related_name='author')
    # assume some other fields here, but not relevant to the discussion.

forms.py

 class AuthorForm(forms.ModelForm):
     class Meta:
         model = Author

 class BaseAuthorFormSet(BaseModelFormSet):
     """will do some clean() here"""
     pass

 AuthorFormSet = modelformset_factory(Author,\
                                    form=AuthorForm, \
                                    formset=BaseAuthorFormSet, extra=1,\
                                    exclude=('user',)
                                    )

views.py

def my_view(request):
    if request.method == 'POST':
        formset = AuthorFormSet(request.POST)
        if form.is_valid(): 
            form.save() # This will cause problems
                        # for the empty form i added,
                       # since user can't  be null.

EDIT

This is what i ended up doing in my view. Wogan’s answer is partly correct, i think it’s just missing the iterating over all the forms part.

for form in formset.forms: 
    form.instance.user = request.user
formset.save()
  • 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-16T04:39:03+00:00Added an answer on May 16, 2026 at 4:39 am

    You can exclude fields in the model form, these will then be excluded from the formset created by modelformset_factory.

    class AuthorForm(forms.ModelForm):
        class Meta:
            model = Author
            exclude = ('user',)
    

    In your view, pass commit=False to the form’s save() method, then set the user field manually:

    def my_view(request):
        if request.method == 'POST':
            formset = AuthorFormSet(request.POST)
            if formset.is_valid(): 
                for author in formset.save(commit=False):
                    author.user = request.user
                    author.save()
                formset.save_m2m() # if your model has many to many relationships you need to call this
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like my iphone app to pre-populate some fields once a user has logged
I have two forms ('table' and 'fields'). The 'fields' form is supposed to pre-populate
I'm trying to pre-populate create defect popup and I'm not clear on what key/values
I would like to pre-populate a UITextfield with the phone number of the current
I have a form that I pre-populate with test data though jQuery. $('INPUT[name=subscription.FirstName]').val('Jef'); but
I am trying to display a Dialog with an EditText field, pre-populate the EditText
I'm importing pre-created Crystal Reports into a report project, and I want to populate
I created a pre-populate SQL Lite DB an added it to my app as
I have to pre-populate two calendars with values. I need it to be in
If you want to pre-populate a database (SQLite) in Android, this is not that

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.