Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to use the technique described in <a href="https://stackoverflow.com/questions/622982/django-passing-custom-form-parameters-to-formset/624013#624013">this post</a> in order to be able to pass parameters in. Credit to that author for an excellent post. You achieve this in several parts:</p> <p><strong>A form aware it is going to pick up additional parameters</strong></p> <p>Example from the linked question:</p> <pre><code>def __init__(self, *args, **kwargs): someeobject = kwargs.pop('someobject') super(ServiceForm, self).__init__(*args, **kwargs) self.fields["somefield"].queryset = ServiceOption.objects.filter( somem2mrel=someobject) </code></pre> <p>Or you can replace the latter code with</p> <pre><code> self.fields["somefield"].initial = someobject </code></pre> <p>Directly, and it works.</p> <p><strong>A curried form initialisation setup</strong>:</p> <pre><code>formset = formset_factory(Someform, extra=3) formset.form = staticmethod(curry(someform, somem2mrel=someobject)) </code></pre> <p>That gets you to passing custom form parameters. Now what you need is:</p> <p><strong>A generator to acquire your different initial parameters</strong></p> <p>I'm using this: </p> <pre><code>def ItemGenerator(Item): i = 0 while i &lt; len(Item): yield Item[i] i += 1 </code></pre> <p>Now, I can do this:</p> <pre><code>iterdefs = ItemGenerator(ListofItems) # pass the different parameters # as an object here formset.form = staticmethod(curry(someform, somem2mrel=iterdefs.next())) </code></pre> <p>Hey presto. Each evaluation of the <code>form</code> method is being evaluated in parts passing in an iterated parameter. We can iterate over what we like, so I'm using that fact to iterate over a set of objects and pass the value of each one in as a different initial parameter.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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