Form Relationships, Multiple Models Using a loop?

2010-06-16 Thread Erich
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):

forms.py:

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 = [
#'answer',
#'note',
#]


views.py

@login_required
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.

...
{% csrf_token %}


{% for dreq in question %}
  {% if dreq.headline %}
{{ dreq.headline }}
  {% endif %}
  {{ dreq.req_num }}: {{ dreq.desc}}
  {% if dreq.needs_answer %}
{{ saq_form.answer }}
   {{ saq_form.note }}
  {% endif %}
{% endfor %}




...


models.py
...
# 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
overkill.

Thanks in advance.

Erich

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Easiest way to pass variables between modelforms (genericforms)

2010-07-06 Thread Erich
I am using create_update.create_object to handle my forms (generic
forms both use modelforms). I have two forms. The second relies on the
pk of the first as a fk. I can't figure out what is the best practice
to A: save the pk as a variable or into the session, then B: pull that
variable either out of the session or another place, such as a global
variable.

I could write the forms manually, but this does not seem to be the
most efficient way, unless create_object was meant for very simple
forms. I have tried using extra_context to call another function, but
I dont know if that is the best way either.

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Easiest way to pass variables between modelforms (genericforms)

2010-07-12 Thread Erich
Derek,

Cool, thanks for that. I wonder if something similar would work on
passing values to a formset...

Erich

On Jul 8, 1:17 am, derek  wrote:
> On Jul 7, 12:44 am, Erich  wrote:
>
> > I am using create_update.create_object to handle my forms (generic
> > forms both use modelforms). I have two forms. The second relies on the
> > pk of the first as a fk. I can't figure out what is the best practice
> > to A: save the pk as a variable or into the session, then B: pull that
> > variable either out of the session or another place, such as a global
> > variable.
>
> > I could write the forms manually, but this does not seem to be the
> > most efficient way, unless create_object was meant for very simple
> > forms. I have tried using extra_context to call another function, but
> > I dont know if that is the best way either.
>
> Maybe have a look 
> at:http://www.petersanchez.com/2008/09/26/django-formwizard-passing-data...

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Form Relationships, Multiple Models Using a loop?

2010-07-13 Thread Erich
I think what I was looking for was inlineformsets. Easy enough i
suppose. We'll see. Anyone interested, see here:
http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#inline-formsets

On Jun 16, 8:35 pm, Erich  wrote:
> 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):
>
> forms.py:
>
> 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 = [
> #            'answer',
> #            'note',
> #            ]
>
> views.py
>
> @login_required
> 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.
>
> ...
> {% csrf_token %}
>
> 
> {% for dreq in question %}
>   {% if dreq.headline %}
>     {{ dreq.headline }}
>   {% endif %}
>   {{ dreq.req_num }}: {{ dreq.desc}}
>   {% if dreq.needs_answer %}
>     {{ saq_form.answer }}
>                        {{ saq_form.note }}
>   {% endif %}
> {% endfor %}
> 
>
> 
> 
> ...
>
> models.py
> ...
> # 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
> overkill.
>
> Thanks in advance.
>
> Erich

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Easiest way to pass variables between modelforms (genericforms)

2010-07-13 Thread Erich
I think I found what I was looking for: inlineformsets.
Here: 
http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#inline-formsets

On Jul 12, 1:56 pm, Erich  wrote:
> Derek,
>
> Cool, thanks for that. I wonder if something similar would work on
> passing values to a formset...
>
> Erich
>
> On Jul 8, 1:17 am, derek  wrote:
>
>
>
> > On Jul 7, 12:44 am, Erich  wrote:
>
> > > I am using create_update.create_object to handle my forms (generic
> > > forms both use modelforms). I have two forms. The second relies on the
> > > pk of the first as a fk. I can't figure out what is the best practice
> > > to A: save the pk as a variable or into the session, then B: pull that
> > > variable either out of the session or another place, such as a global
> > > variable.
>
> > > I could write the forms manually, but this does not seem to be the
> > > most efficient way, unless create_object was meant for very simple
> > > forms. I have tried using extra_context to call another function, but
> > > I dont know if that is the best way either.
>
> > Maybe have a look 
> > at:http://www.petersanchez.com/2008/09/26/django-formwizard-passing-data...

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Easiest way to pass variables between modelforms (genericforms)

2010-07-15 Thread Erich
Inline FormSets work great for somethings that I'm doing, but to solve
the problem that I had originally I passed a variable to the template
that i specified as a value in a hidden field. I don't know if that is
bad form or not, but it's working.

On Jul 13, 4:39 pm, Erich  wrote:
> I think I found what I was looking for: inlineformsets.
> Here:http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#inline-...
>
> On Jul 12, 1:56 pm, Erich  wrote:
>
>
>
> > Derek,
>
> > Cool, thanks for that. I wonder if something similar would work on
> > passing values to a formset...
>
> > Erich
>
> > On Jul 8, 1:17 am, derek  wrote:
>
> > > On Jul 7, 12:44 am, Erich  wrote:
>
> > > > I am using create_update.create_object to handle my forms (generic
> > > > forms both use modelforms). I have two forms. The second relies on the
> > > > pk of the first as a fk. I can't figure out what is the best practice
> > > > to A: save the pk as a variable or into the session, then B: pull that
> > > > variable either out of the session or another place, such as a global
> > > > variable.
>
> > > > I could write the forms manually, but this does not seem to be the
> > > > most efficient way, unless create_object was meant for very simple
> > > > forms. I have tried using extra_context to call another function, but
> > > > I dont know if that is the best way either.
>
> > > Maybe have a look 
> > > at:http://www.petersanchez.com/2008/09/26/django-formwizard-passing-data...

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.