Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango ModelForm Instantiation after Form Creation
    primarykey
    data
    text
    <p>I have a form that consists of a response for each entry in another model. At the time the form is generated the response to each item may or may not exist. I need a form that allows me to update the response if it exists and create it with the form post data if it doesn't exist.</p> <p>Currently I am iterating through a range and creating my forms with the post data:</p> <pre><code>forms = [SpecialNoteForm(request.POST, prefix=str(x), ) for x in rang(1,3)] </code></pre> <p>I am doing this because I don't know how else to access the form data cleanly in order to identify the object that the form should be instantiated with. I tried doing something like this after the forms list was created because i can then access the form data:</p> <pre><code>for form in forms: try: instance = SpecialNote.objects.get(flag=form["flag"].data, host=form["host"].data) form.instance = instance form.save() </code></pre> <p>The errors on the form persist after I do this, however. I need a way of accessing the data I need to instantiate the object at the time of form creation or a way of re-evaluating the form after i've attached an instance to it.</p> <p><strong>EDIT</strong></p> <p>I ran into the same problem with model formsets as I did with my initial approach--I don't know how to instantiate the forms while at the same time allowing for intial values on forms that don't have an instance. I don't want to create all of the model instances before hand because it is import whether or not the user has submitted these with the required fields filled in.</p> <p>My current approach is still using the model forms:</p> <pre><code>forms = [] for n in form_range(request.POST): # calculates number of forms based on post data try: instance = SpecialNote.objects.get(flag=request.POST.get('%s'%n+'-flag'), host=request.POST.get('%s'%n+'-host')) except: instance = None forms.append(SpecialNoteForm(request.POST, prefix=str(n), instance=instance)) for form in forms: if form.is_valid(): form.save() </code></pre> <p>In summary, the problem with formsets is I don't know how to properly instantiate the forms without having them be queriable, i.e. already in the database. The problem with using regular model forms and a prefix is that getting the objects that i need to instantiate them with is messy (as you can see from my current approach). I'm looking for a solution to either of these two problems.</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. 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