Form data inside another Form
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': "" } 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-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Form data inside another Form
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 <[EMAIL PROTECTED]> wrote: > > > On Fri, 2007-12-21 at 18:38 +, 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.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-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Best Practice for Ajax and Django. Please share your thoughts.
ou use, > > > I'd just like to know your opinions on the best way to go to handle it > > > on the server side. > > > > > I'm gonna tell you how I do it, then I'd really appreciate if you > > > could share the way you do it. There are probably many good > > > approaches, but maybe we could find some common ground that could for > > > example be put online on the wiki. > > > > > What I do is mostly cosmetic to keep things tidy: > > > > > 1) I put all my ajax views in a separate file 'ajax-views.py'. By > > > "ajax views" I mean all views that are called by a javascript, as > > > opposed to the traditional views which are called through the > > > browser's address bar. > > > The structure then looks like this: > > > myapp > > >|_ ajax-views.py > > >|_ models.py > > >|_ urls.py > > >|_ views.py > > > > > 2) In the URLConf I also separate traditional views from ajax views: > > > > > from django.conf.urls.defaults import * > > > > > # Traditional views > > > urlpatterns = patterns('myapp.views', > > >url(r'^$', 'home'), > > >url(r'^news/$', 'list_news'), > > > ) > > > > > # AJAX views > > > urlpatterns += patterns('myapp.ajax-views', # Don't forget the > > > '+=' sign here. > > >url(r'^ajax/news/latest/$', 'ajax_latest_news'), > > >url(r'^ajax/news/add/$', 'ajax_add_news'), > > > ) > > > > > Note that I also add the prefix "ajax/" in the URLs for the ajax > > > views. > > > > > 3) The ajax views can look like this: > > > > > from django.utils import simplejson > > > > > def ajax_latest_news(request): > > > ... > > > latest_news = ... > > > json = simplejson.dumps(latest_news) > > > return HttpResponse(json, mimetype='application/json') > > > > > def ajax_add_news(request): > > > ... > > > results = {'success':True} > > > json = simplejson.dumps(results) > > > return HttpResponse(json, mimetype='application/json') > > > > > Please let me know your thoughts ;) > > > Cheers! > > > > > Julien > > > > -- > > To pretend, I actually do the thing: I have therefore only pretended to > pretend. > > - Jacques Derrida > > > -- --- 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-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Newfroms - How to get the id for a field
Hi, {{ fields }} is rendered by the as_widget() method of the BoundField class. Jorge On Feb 15, 2008 8:58 PM, Rajesh Dhawan <[EMAIL PROTECTED]> wrote: > > > > On Feb 14, 4:15 am, shabda <[EMAIL PROTECTED]> wrote: > > When using newforms, I want to get the id set on the input field. > > I am doing something like, > > {% for field in form %} > > {{ field }} > > ... > > > > Now this {{field}} will render as something like, > > > > > > > > Withing the template how can I access the value for id. If this is not > > possible where in code does Django set the value for id? > > Try {{ field.auto_id }} > > > > > -- --- 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-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Shell reloading
Hi, I'm using Django on windows. Is it possible to, once inside the Django Shell (manage.py shell) while testing some bint and changing the code in a model method to have it available to the shell? Kind of autoreload thing. It only work by exiting the shell and entering again. But then have to import everything again. Thanks 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-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---