List Info

Thread: Form data inside another Form




Form data inside another Form
user name
2007-12-21 12:38:03
Hi,

I'm a newby to django and python

I'm trying to create a form (master / detail) editable all the way.
I'm able to post data in the following format:

request.data = {
   'id9;: NN,
   'name': "John Doe',
  ; 'detail1.0.cod9;: 'XX9;,
   'detail1.0.descr': "abc",
   'detail1.1.cod9;: 'YY9;,
 ;  'detail1.1.descr': "abc",
   'detail1.2.cod9;: 'ZZ9;,
   'detail1.2.descr': "abc",
   . .
   ..
   'detail1.NN.cod': -----',
 ;  'detail1.NN.descr': "----"
}

In the view that handles de POST
i';m using the following code to isolate the detail data

data = request.POST
details = {}
for key, value in data.iteritems():
    m = re.match('(w+).(d+).(.*)';, key)
    if m is not None:
        k, i, f = m.group(1), int(m.group(2)), m.group(3)
        if not d.has_key(k):
     ;       d[k] = dict()
        if not d[k].has_key(i):
          ;  d[k][i] = {}
     ;      
     ;   d[k][i][f] = value

it transforms POST data in something like
{ detail0 : {
  '0'; : { id: XX, descr: 'abc' },
  '1'; : { id: YY, ....},
  '2';:  { id: ZZ , ....}
}}

and so on, so each row could be fed into a Detail0Form instances for validation
The above code seem very clumsy to me, but it was the only way i was able to do it.
Is there an easy way to write it down and use a List instead of a Dict. (I wasn't able to do it with a List)

Thanks,

Merry Christmas  to all

Jorge Sousa




--
-------------------------------
Jorge Sousa
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users"; group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to django-users-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Re: Form data inside another Form
country flaguser name
Australia
2007-12-21 22:44:01

On Fri, 2007-12-21 at 18:38 +0000, Jorge Sousa wrote:
> Hi,
> 
> I'm a newby to django and python 
> 
> I'm trying to create a form (master / detail) editable
all the way. 
> I'm able to post data in the following format: 
> 
> request.data = {
>    'id': NN, 
>    'name': "John Doe',
>    'detail1.0.cod': 'XX',
>    'detail1.0.descr': "abc",
>    'detail1.1.cod': 'YY',
>    'detail1.1.descr': "abc",
>    'detail1.2.cod': 'ZZ',
>    'detail1.2.descr': "abc",
>    . . 
>    .. 
>    'detail1.NN.cod': -----',
>    'detail1.NN.descr': "----"
> }

If you could slightly rename your submission fields, there
is indeed an
easier way to do this. I'm assuming you are using Django's
"newforms"
package to create a form class of some kind. The
"prefix" attribute that
you can optionally pass to a form's __init__ method can be
used to tweak
the names expected by a form's elements.

Also, since you can happily pass extra information to a form
(it will
just ignore it), you will end up with something like the
following:

        # Assume MasterForm and DetailForm are your form
classes
        
        def my_view(request, ...):
           master = MasterForm(request.POST)
           detail_forms = []
           for i in range(XX):	# work out XX somehow
              detail_forms.append(DetailForm(request.POST,
        prefix=str(i))
           # ... (now check is_valid(), etc, as per normal)
        
To see how the form element should be named here, have a
look at [1],
for example. That's part of the test suite and shows the
default naming.
You could also override the add_prefix() in your form so
that it returns
something in the format you're expecting from the form
submission.

[1]
http://code.djangop
roject.com/browser/django/trunk/tests/regressiontests/forms/
forms.py#L1228

This might all take more than just a couple of minutes to
master, since
you said you were just starting out with Django and Python.
Like
anything new, doing advanced stuff will require a bit of
patience and
experimentation, so just try things out and you'll gradually
close in on
a solution.

Regards,
Malcolm

-- 
No one is listening until you make a mistake. 
http://www.pointy-s
tick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Form data inside another Form
user name
2007-12-28 04:31:26
Thanks Malcom,

I've used your solution and it simplified 200% what i was trying to accomplish. And guess what, i was able to do what i wanted.
There are some things i don't quite understand in python and django but with time and patience i'll get there. but i'm loving this framework!!!

Thanks again for showing me the right "WAY" to do it.
Maybe i'll write a mini guide about what i ended up. Its a "complex form" (to me) that maps to 4 tables.

Happy new year to all.
Jorge Sousa

On Dec 22, 2007 4:44 AM, Malcolm Tredinnick < malcolmpointy-stick.com">malcolmpointy-stick.com> wrote:


On Fri, 2007-12-21 at 18:38 +0000, Jorge Sousa wrote:
>; Hi,
>
&gt; I'm a newby to django and python
>;
> I'm trying to create a form (master / detail) editable all the way.
>; I'm able to post data in the following format:
&gt;
> request.data = {
>    ';id': NN,
>    ';name': "John Doe',
>    ';detail1.0.cod': 'XX9;,
>    ';detail1.0.descr': "abc&quot;,
>    ';detail1.1.cod': 'YY9;,
>    ';detail1.1.descr': "abc&quot;,
>    ';detail1.2.cod': 'ZZ9;,
>    '; detail1.2.descr';: "abc&quot;,
>    . .
>    ..
&gt;    ';detail1.NN.cod': -----',
>    ';detail1.NN.descr': "----"
> }

If you could slightly rename your submission fields, there is indeed an
easier way to do this. I'm assuming you are using Django';s "newforms"
package to create a form class of some kind. The "prefix" attribute that
you can optionally pass to a form's __init__ method can be used to tweak
the names expected by a form's elements.

Also, since you can happily pass extra information to a form (it will
just ignore it), you will end up with something like the following:

       # Assume MasterForm and DetailForm are your form classes

       def my_view(request, ...):
          master = MasterForm(request.POST)
          detail_forms = []
          for i in range(XX):  # work out XX somehow
             detail_forms.append(DetailForm( request.POST,
       prefix=str(i))
          # ... (now check is_valid(), etc, as per normal)

To see how the form element should be named here, have a look at [1],
for example. That's part of the test suite and shows the default naming.
You could also override the add_prefix() in your form so that it returns
something in the format you're expecting from the form submission.

[1]
http://code.djangoproject.com/browser/django/trunk/tests/regressiontests/forms/forms.py#L1228

This might all take more than just a couple of minutes to master, since
you said you were just starting out with Django and Python. Like
anything new, doing advanced stuff will require a bit of patience and
experimentation, so just try things out and you'll gradually close in on
a solution.

Regards,
Malcolm

--
No one is listening until you make a mistake.
http://www.pointy-stick.com/blog/







--
-------------------------------
Jorge Sousa
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users"; group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to django-users-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )