Login

Multiple models display form

Author:
lbolognini
Posted:
July 18, 2007
Language:
Python
Version:
.96
Tags:
form
Score:
-2 (after 2 ratings)

You have an update form with many fields and want to populate it with data coming from multiple models.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def change_house_let(request, object_id):
    if request.method == "POST":
        new_data = request.POST.copy()
        form = HouseLetForm(new_data)
        
        if form.is_valid():
            pass
        else:
            pass
    else:
        h = HouseLet.objects.get(pk=object_id)
        p = Property.objects.get(pk=object_id)
        a = Ad.objects.get(pk=object_id)
        
        d = {}
        
        d.update(vars(h))
        d.update(vars(p))
        d.update(vars(a))
        
        context = dict(form=HouseLetForm(initial=d))

    return render_to_response("house_let.html", context)

More like this

  1. MultiFormWizard by flashingpumpkin 6 years, 1 month ago
  2. Adding Fields to Registration Form [or Multiple Inheritance of forms] by simonkagwe 3 years, 9 months ago
  3. Multiple Choice model field by danielroseman 6 years, 7 months ago
  4. Convert multiple select for m2m to multiple checkboxes in django admin form by abidibo 2 years, 2 months ago
  5. Improved Multiple Model Files by softmechanics 5 years, 6 months ago

Comments

miracle2k (on July 18, 2007):

If you're models are related to each other via a Foreignkey, beware. They're (autoincremented) primary keys could in theory be out of sync. I had the same thing just happen to me.

You may want to consider doing something like this instead:

p = Property.objects.get(pk=object_id) h = HouseLet.objects.get(property=p.id)

Or differently, whatever your exact relationships are.

#

Please login first before commenting.