Electronics & Programming


Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Form Relationships, Multiple Models Using a loop? [RSS Feed]

#1 June 17, 2010 04:40:59

Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Form Relationships, Multiple Models Using a loop?

First time poster, so go easy on me...

I need to create a form that will display a heading / description that
comes from one model (DReq) which I can get to display in a for loop.
Each dreq.desc is dependent on the results of a var that gets set and
read in from the user's session. Then I need to add a couple form
fields (from Saq) to each one that will relate to dreq.req_number
(also displayed alongside the desc) so that I can save the user's
input to the db (the DReq and MSaq are related as a MTM through
another model Saq).

The best way I can figure out how to do this is to use some sort of
for loop that will display each item and the respective form fields. I
have gotten that far by going the template route (see below). My
problem is that I do not know how to relate them to each other. Below
is some of what I have come up with. If all else fails I will just
create four (there are only four possible configurations for the form,
but it seems like this would be more elegant in a loop, somehow
relating them) individual forms to handle the task.

Here is what I have come up with so far (I know this together doesn't
work, it is just what I have tried in the various MTV places):


class MSForm(forms.Form):
answer = forms.Charfield(widget=forms.NullBooleanSelect)
note = forms.Charfield(widget=forms.TextArea)
# Perhaps use a ModelForm?
# class Meta:
# model = DReq
# fields =


def saq(request):
current_saq = request.session.get('current_saq')
question = DReq.objects.filter(s_types=current_saq)

# Probably not the right place for the loop, but i was trying
something different
for dreq in question:
if question.headline:
req_headline = question.headline
req_number = dreq.req_num
if dreq.needs_answer:
saq_form = MSForm()

if request.method == 'POST':
saq_form = MSForm(request.POST)

if saq_form.is_valid():
# Query that pulls the requirement and assigns the correct
answer to it.

template.html -- This will display the form with the proper
information (from a different views.py version), but the form inputs
and the desc/reqs are not related in any way. Closest I've come except
it obviously doesn't work.

<form action="" method="post">{% csrf_token %}

{% for dreq in question %}
{% if dreq.headline %}
<li>{{ dreq.headline }}</li>
{% endif %}
<li>{{ dreq.req_num }}: {{ dreq.desc}}</li>
{% if dreq.needs_answer %}
<div class="field">{{ saq_form.answer }}
{{ saq_form.note }}</div>
{% endif %}
{% endfor %}

<input type="submit" value="Submit">

# This is where the desc text comes from
class DReq(models.Model):
req_num = models.CharField(max_length=5, blank=False)
headline = models.CharField(max_length=200, blank=True)
description = models.CharField(max_length=400, blank=False)
needs_answer = models.BooleanField(blank=False)
s_types = models.ManyToManyField(SType)

class MSaq(models.Model):
complete = models.BooleanField(blank=True)
d_req = models.ManyToManyField(DReq, through='Saq')

# This is where the data needs to be stored and related to
class Saq(models.Model):
d_req = models.ForeignKey(DReq)
m_saq = models.ForeignKey(MSaq)
answer = models.NullBooleanField(blank=False)
note = models.CharField(max_length=200, blank=True)

I have redacted some of the names to protect the innocent, I hope it
is still easy to follow. I feel like I am missing something very
basic, but I just can't wrap my head around what.

So, I just need to link the descriptions (which get iterated and
displayed just fine) to the form fields that get displayed with them,
so I can write the responses to the db. I hope this isn't info

Thanks in advance.


You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.


  • Root
  • » Django
  • » Form Relationships, Multiple Models Using a loop? [RSS Feed]

Board footer

Moderator control

Enjoy the 14th of December

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support