Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Probably formset factory and inline formset should solve your problem... You can modify or override form fields created from models, For sub models, you can use inline formsets...</p> <p><a href="http://docs.djangoproject.com/en/1.2/topics/forms/modelforms/#inline-formsets" rel="nofollow">Formsets and inline formsets...</a></p> <p>A possible solution:</p> <p>in your form definition:</p> <pre><code>class CarInfoFrm(forms.ModelForm): class Meta: model = CarInfo fields = (....) carInfoForm = inlineformset_factory(Complaint, CarInfo, form=carInfoFrm,) CustomerForm = inlineformset_factory(Complaint, Customer, form=carInfoFrm,) </code></pre> <p>In your view:</p> <pre><code>complaint = Complaint() carInfo = CarInfo() customer = Customer() cus_form = CustomerForm(parameters, instance=complaint) car_form = CarInfoForm(parameters, instance=complaint) comp_form = ComplaintForm(parameters, instance=complaint) if cus_form.is_valid() and ...... : comp = comp_form.save(commit=False)#do not save it yet comp.&lt;attr&gt; = "some_value" #you can edit your data before save... comp.save() car = car_form(commit=False) # do as complaint form... edit and save... </code></pre> <p>EDIT: I make a misteke when defining instance parameter while saving inlineforms. So i correct it...</p> <p>When you update existing records, you will not have problem, but it is better to use it like:</p> <pre><code>if comp_form.is_valid(): comp = comp_form.save(commit=False) comp.&lt;attr&gt; = "some_value" comp.save() if car_form.is_valid(): # edit if neccessary then save... if cust_form.is_Valid(): # edit if neccessary then save... </code></pre> <p>What makes easier is, when you define your form, you set a parent form via a foreign key</p> <pre><code>carInfoForm = inlineformset_factory(Complaint, CarInfo, form=carInfoFrm,) </code></pre> <p>And when you update using inline form, you initialize it with your parent Compalaint record, </p> <pre><code>car_form = CarInfoForm(parameters, instance=complaint) </code></pre> <p>So, car_form do not accept an carInfo instance, but a complaint instance(that was my mistake in my first answer, so i correct it). if it creates a new record, it automatically binds it to related complaint record. If it is an update, it only updated fields that you want.</p> <p>For me iti is better to use moehods of the framework instead of writing your owns. By doing this, you will guarantee to have all your validation checks made by django.</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.
    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.
 

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