Re: view/form with fields from multiple models
Hey, I've been a little distracted but I'm back on to this now... Thanks for the information, I'm going to try it out now (assuming no more interruptions!) and I'll get back to you on how I get on... On Jul 24, 8:55 pm, Matthias Kestenholz wrote: > On Fri, Jul 24, 2009 at 10:46 AM, Benjamin Wohlwend > wrote: > > > On Jul 24, 10:37 am, Benjamin Wohlwend wrote: > > >> if all(f.is_valid() for f in (form1, form2, form3)): > >> # ok, save > > > Oops, forgot to mention that I had to implement my own version of all > > (), which doesn't short cut: > > > def really_all(iter): > > all_true = True > > for val in iter: > > if not val: > > all_true = False > > return all_true > > There is a function in Django proper which does exactly that: > django.forms.formsets.all_valid > > Since is_valid works the same for forms and formsets you can use this > for plain forms too. > > Matthias --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: view/form with fields from multiple models
On Fri, Jul 24, 2009 at 10:46 AM, Benjamin Wohlwend wrote: > > On Jul 24, 10:37 am, Benjamin Wohlwend wrote: > >> >> if all(f.is_valid() for f in (form1, form2, form3)): >> # ok, save >> > > Oops, forgot to mention that I had to implement my own version of all > (), which doesn't short cut: > > def really_all(iter): > all_true = True > for val in iter: > if not val: > all_true = False > return all_true There is a function in Django proper which does exactly that: django.forms.formsets.all_valid Since is_valid works the same for forms and formsets you can use this for plain forms too. Matthias --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: view/form with fields from multiple models
On Jul 24, 10:37 am, Benjamin Wohlwend wrote: > > if all(f.is_valid() for f in (form1, form2, form3)): > # ok, save > Oops, forgot to mention that I had to implement my own version of all (), which doesn't short cut: def really_all(iter): all_true = True for val in iter: if not val: all_true = False return all_true --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: view/form with fields from multiple models
Hi, On Jul 24, 8:41 am, Matthias Kestenholz wrote: > if request.method == 'POST': > # initialize forms with request.POST and prefix > if form1.is_valid() and form2.is_valid() and form3.is_valid(): > # save the data from the individual forms > else: > # > Just a little remark, as I hit this stumbling block a couple of days ago: if you validate the forms like this (f1.is_valid() and f2.is_valid () and ...) and your first form is not valid, Python will short cut the if statement and not call is_valid on the other forms, which isn't generally what you want in this situation. This works better: if all(f.is_valid() for f in (form1, form2, form3)): # ok, save Regards, Benjamin --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: view/form with fields from multiple models
On Fri, Jul 24, 2009 at 5:32 AM, sico wrote: > > Hi, > > I want to be able to edit fields from several different models on the > same form/view. > > The models are related one-to-one... can they be defined as "Inline" > forms in the admin system?? The documentation doesn't make any > mention of using inline forms for anything except parent child > relations so I'm thinking this may cause issues. > > Whats the best way to handle this in a custom view? Do I just simply > define forms for each model, pass them in separately to be displayed > in the template and then validate and save them individually on > saving? The fields would need to be clearly distinguishable to which > model they belong using a prefix which is something that formsets use > to distinguish themselves Yes, you'd do it this way. Forms (and formsets too) take a prefix argument which you can use to differentiate between the forms. I think you do not want to validate and save them individually though (I'm sorry if I did not understand you), you probably do not want to save anything until all forms have passed validation: if request.method == 'POST': # initialize forms with request.POST and prefix if form1.is_valid() and form2.is_valid() and form3.is_valid(): # save the data from the individual forms else: # return render_to_response('template.html', {'form1': form, 'form2': form, 'form3': form}) > (I actually want to be able to edit multiple records of these in a > grid, but need to be able to edit one first!) Then you'll want to look into formsets, and use their prefix parameter too to make sure that the field names of different formsets don't clash. Matthias --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
view/form with fields from multiple models
Hi, I want to be able to edit fields from several different models on the same form/view. The models are related one-to-one... can they be defined as "Inline" forms in the admin system?? The documentation doesn't make any mention of using inline forms for anything except parent child relations so I'm thinking this may cause issues. Whats the best way to handle this in a custom view? Do I just simply define forms for each model, pass them in separately to be displayed in the template and then validate and save them individually on saving? The fields would need to be clearly distinguishable to which model they belong using a prefix which is something that formsets use to distinguish themselves so... what is the best way to allow editing of fields from multiple one-to-one models at once. thanks! Simon p.s. (I actually want to be able to edit multiple records of these in a grid, but need to be able to edit one first!) --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---