Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can change your Complaint model's complaint_date to something like this</p> <p><code>complaint_date = models.DateField(default=datetime.date.today())</code> </p> <p>that way you can get rid of </p> <p><code>parameters['complaint_date'] = get_current_date()</code></p> <p>As for your multiform view you can use an unbound forms for you desired behavior. </p> <p>By excluding the fk to the car and customer on the complaint form, the forms should validate. Check the .is_valid() of all 3 forms at the same time and then save the 2 forms that your complaint object is dependent on, create the complaint object with out commit to database (commit=False), add the id's of the customer and car to that object, then save.</p> <p>in your view.</p> <pre><code>def make(request): if request.method == 'POST': customer_form = CustomerForm(request.POST) carInfo_form = CarInfoForm(request.POST) form = ComplaintForm(request.POST) if form.is_valid() and customer_form.is_valid() and carInfo_form.is_valid(): car_instance = carInfo_form.save() customer_instance = customer_form.save() complaint_instance = form.save(commit=False) complaint_instance.car_info = car_instance complaint_instance.customer_info = customer_instance complaint_instance.save() return index(request) else: form = ComplaintForm() carInfo_form = CarInfoForm() customer_form = CustomerForm() context = { 'complaint_form' : form, 'customer_form' : customer_form, 'carInfo' : carInfo_form, } return render_to_response('complaints/make_complaint.html', context, context_instance=RequestContext(request)) </code></pre> <p>edit:</p> <p><strong>models look like this:</strong></p> <pre><code>class CarInfo(models.Model): some_car_info = models.CharField() class Customer(models.Model): some_customer_info = models.CharField() class Complaint(models.Model): car_info = models.ForeignKey(CarInfo) customer_info = models.ForeignKey(Customer) some_complaint_info = models.CharField() </code></pre> <p><strong>forms.py should look like this:</strong></p> <pre><code>class CarInfoForm(forms.ModelForm): class Meta: model = CarInfo class CustomerForm(forms.ModelForm): class Meta: model = Customer class ComplaintForm(forms.ModelForm): class Meta: model = Complaint exclude = ('car_info', 'customer_info',) # or include = ('some_complaint_info',) </code></pre> <p><strong>Lets walk through the view I wrote out above:</strong> <a href="http://docs.djangoproject.com/en/dev/topics/forms/#using-a-form-in-a-view" rel="nofollow"><code>form in view docs</code></a></p> <ul> <li><p>On first pass, there is no request.method so we create 3 <a href="http://docs.djangoproject.com/en/dev/ref/forms/api/#ref-forms-api-bound-unbound" rel="nofollow">unbound</a> forms.</p> <pre><code>else: form = ComplaintForm() carInfo_form = CarInfoForm() customer_form = CustomerForm() </code></pre></li> <li><p>these forms are passed to the template and rendered.</p></li> <li><p>When the view is called again with request.method == "POST" evaluating true, we create the 3 bound form instances using the data from our request.POST.</p> <pre><code>if request.method == 'POST': customer_form = CustomerForm(request.POST) carInfo_form = CarInfoForm(request.POST) form = ComplaintForm(request.POST) </code></pre></li> <li><p>Next we call the .is_valid() method on each <strong>form</strong>. In our example because we excluded the 'customer_info' and 'car_info' foreign key fields in our complaint modelform, each form is only checking to see that the char input field validates.</p></li> <li><p>If validation all passes then we can start saving our forms to models and this where we need to be careful about populating our complaint's required fk's:</p> <pre><code> if form.is_valid() and customer_form.is_valid() and carInfo_form.is_valid(): car_instance = carInfo_form.save() customer_instance = customer_form.save() </code></pre></li> <li><p>With those 2 forms we can call the .save() as usual. We will however assign the return value to car_instance and customer_instance. These will contain the instances of the CarInfo and Customer models we just created using the .save() method on the form.</p></li> <li><p>Next, using the <a href="http://docs.djangoproject.com/en/1.2/topics/forms/modelforms/#the-save-method" rel="nofollow"><code>commit=False</code></a> argument in the .save() method, we are able to create an object from the bound form (containing the request.POST data) and not save it to the database.</p> <pre><code> complaint_instance = form.save(commit=False) complaint_instance.car_info = car_instance complaint_instance.customer_info = customer_instance complaint_instance.save() </code></pre></li> <li><p>To make this clearer, you could also have created a new Complaint object like this:</p> <pre><code>complaint_info = form.cleaned_data.get('some_complaint_info') complaint_instance = Complaint(car_info=car_instance, customer_info=customer_instance, some_complaint_info=some_complaint_info) complaint_instance.save() </code></pre></li> <li><p>render </p></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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