Note that there are some explanatory texts on larger screens.

plurals
  1. POdjango inline form with custom forms
    primarykey
    data
    text
    <p>Hi I have a domain model, used in Django app, which I'd like to present on a single form. I've created my application with custom ModelForms (not much changes, some fields excluded etc). The model's dependencies are as follows:</p> <pre><code>Complaint \ .--- CarInfo .--- Customer </code></pre> <p>My view function looks like this:</p> <pre><code>def make(request): if request.method == 'POST': parameters = copy.copy(request.POST) complaint = Complaint() carInfo = CarInfo() customer = Customer() customer_form = CustomerForm(parameters, instance=customer) carInfo_form = CarInfoForm(parameters, instance=carInfo) parameters['complaint_date'] = get_current_date() parameters['customer'] = 1 # dummy value to allow validation success parameters['car_info'] = 1 # dummy value to allow validation success form = ComplaintForm(parameters, instance=complaint) if form.is_valid() and customer_form.is_valid() and carInfo_form.is_valid(): carInfo_form.save() customer_form.save() parameters['customer'] = customer.id parameters['car_info'] = carInfo.id form = ComplaintForm(parameters, instance=complaint) form.save() return index(request) else: form = ComplaintForm() carInfo_form = CarInfoForm() customer_form = CustomerForm() return render_to_response('complaints/make_complaint.html', {'complaint_form' : form, 'customer_form' : customer_form, 'carInfo' : carInfo_form}) </code></pre> <p>I don't like this approach too much, moreover it doesn't work in all environments -thou I haven't found the reason for it not working. I've been looking into fixing this code a bit and found something like <strong>inline formset</strong> (http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#inline-formsets). This solution seems ok, but since my forms are custom tailored I can use it.</p> <p>Perhaps someone could offer me some advice on how to properly solve such case. Cleaner solutions are much appreciated.</p> <p><strong>EDITED</strong> There is a case for me, where this solutions just doesn't work. Despite of setting dummy values on foreign keys, when I call is_valid() I get FALSE, with error message saying that these fields are not set. I'm observing this problem with django 1.2.5 - it occurs on server I intent to run this app, however my laptop (also django 1.2.5) doesn't have this problem.</p>
    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.
 

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