Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango - Adding initial value to a formset
    text
    copied!<p>I have a many-to-many relationship between two classes (Lesson and Student), with an intermediary class (Evaluation).</p> <p>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.</p> <p>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.</p> <p>The error I receive is as follows:</p> <pre><code>__init__() got an unexpected keyword argument 'initial' </code></pre> <p>My actual view (with my attempt at adding the initial data removed) looks like this:</p> <pre><code>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)) </code></pre> <p>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:</p> <pre><code>initial={'user': request.user}, </code></pre> <p>Any advice appreciated</p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload