Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are quite a few things wrong here, in addition to the QuerySet issue Arnaud points to.</p> <p>Firstly, when you instantiate the form in response to a GET, you do <code>contractForm = ContractForm(user)</code>. But the first parameter to a form instantiation (as you correctly do in the POST) is the posted data - and you haven't altered the form's <code>__init__</code> signature.</p> <p>Secondly, in the <code>__init__</code> itself, you reference a <code>user</code> variable: but you haven't actually got that variable from anywhere. You can't magically reference variables without receiving them from somewhere.</p> <p>So to fix those errors, you need to change the init method as follows:</p> <pre><code>def __init__(self, *args, **kwargs): user = kwargs.pop('user') super(ContractForm, self).__init__(*args, **kwargs) self.fields['client'].queryset=Client.objects.filter(user=user) </code></pre> <p>and instantiate the form in the view like this:</p> <pre><code>contractForm = ContractForm(user=request.user) </code></pre> <p>Thirdly, although this is not related to the problem you post, you will never see any errors on this form, because you don't check it is valid in the POST section - you need to check <code>if contractForm.is_valid()</code>, and if not fall through to the final <code>render_to_response</code> (which needs to be unindented one level).</p> <p>Also, you are ignoring the whole point of having a form, by setting your model direct from <code>request.POST</code>. You should be using the contents of <code>contractForm.cleaned_data</code> to create the Contract instance. </p> <p>And finally, you should investigate whether a ModelForm would meet your needs better.</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