Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When I used <code>extra</code> on formsets in similar situation I ended up having to include all the required fields from the model in the form, as HiddenInputs. A bit ugly but it worked, curious if anyone has a hack-around.</p> <p><strong>edit</strong><br> I was confused when I wrote above, I'd just been working on formsets using <code>extra</code> with <code>initial</code> to pre-fill the extra forms and also I hadn't fully got all the details of your questions.</p> <p>If I understand correctly, where you instantiate the <code>LineFormSet</code>s in <code>add_fields</code> each of those will point to the same <code>Result</code> instance?</p> <p>In this case you don't really want to supply <code>result</code> in <code>initial</code> due to the problems you're having. Instead you could remove that field from the LineForm model-form altogether and customise the <code>LineFormSet</code> class something like:</p> <pre><code>class LineFormSet(forms.BaseModelFormSet): # whatever other code you have in it already # ... # ... def __init__(self, result, *args, **kwargs): super(LineFormSet, self).__init__(*args, **kwargs) self.result = result def save_new(self, form, commit=True): instance = form.save(commit=False) instance.result = self.result if commit: instance.save() return instance def save_existing(self, form, instance, commit=True): return self.save_new(form, commit) </code></pre> <p>(this should be ok in Django 1.3 and 1.4, not sure other versions)</p> <p>so the relevant part of your <code>add_fields</code> method would look like:</p> <pre><code> form.nested = [ LineFormSet( result = instance, queryset = q, #data=self.data, instance = instance, prefix = 'LINES_%s' % pk_value)] prefix = 'lines-%s' % pk_value, )] </code></pre>
    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.
 

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