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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:12:20+00:00 2026-05-23T19:12:20+00:00

I have a many-to-many relationship between two classes (Lesson and Student), with an intermediary

  • 0

I have a many-to-many relationship between two classes (Lesson and Student), with an intermediary class (Evaluation).

I am trying to set up a form which will allow me to add a lesson with students and the related evaluation data. I can get all of the fields I want to display correctly, however I also need to set an initial value behind the scenes (the current user), as it does not make sense to have it in the form.

I have tried following the docs but I think I have a syntax error in the way I am passing the data to the formset.

The error I receive is as follows:

__init__() got an unexpected keyword argument 'initial'

My actual view (with my attempt at adding the initial data removed) looks like this:

def addlesson(request):
LessonFormset = inlineformset_factory(Lesson, Evaluation, exclude=('user',), max_num=5)
if request.method == 'POST':
    lesson = Lesson(user=request.user)
    form = LessonForm(request.POST, instance=lesson, user = request.user)
    formset = LessonFormset(request.POST, instance = lesson)
    if form.is_valid() and formset.is_valid():
        form.save()
        formset.save()
        return HttpResponseRedirect("/")
else:
    form = LessonForm(user = request.user)
    formset = LessonFormset()
return render_to_response("addlesson.html", {
    'form': form,
    'formset' : formset,
}, context_instance=RequestContext(request))

Could anyone show me to correct syntax to use to set the current user in the formset?
This is what I had before but it was giving me the error at the start of my post:

initial={'user': request.user},

Any advice 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-05-23T19:12:21+00:00Added an answer on May 23, 2026 at 7:12 pm

    It’s not clear to me why you are using a formset when it looks like you only want to add one row. A regular form would have been how I would do it if there was only one row. But, here’s how I set the default value in a formset.

    I exclude the field, just like you already have in your code. Then:

    if form.is_valid() and formset.is_valid():
        form.save()
        models = formset.save(commit=False)
        for i in models:
            i.user = request.user
            i.save()
        return HttpResponseRedirect("/")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple one-to-many (models.ForeignKey) relationship between two of my model classes: class
I have a one-to-many relationship between two classes for this situation. I have a
I have two domain classes with a many-2-many relationship between them, e.g. User and
I have defined a many-to-many relationship between my two entity classes User and Permission.
I have a fairly simple Many to One relationship between two classes: @Entity public
I have a many-to-many relationship between two tables, let's say Friends and Foods. If
I have two objects (class, student) in a database with a many to many
I have a many-to-many relationship between two objects with a join table. I need
I have a many-to-many relationship between the two entities called events and artists, both
I have a many-to-many relationship defined between two entities using an association table and

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.