Re: FormSet issue. Pre-populating from queryset
Malcolm, Thanks for the comment. I truly understand where this is coming from. This issue is completely my issue of taking things for granted. I have been coding around in Django for a while now (hooked on in the 0.95 days), and have been coding most of the form stuff (including inlines) all by hand. When I noticed the Formset class, I tried to make it work in the wrong place. Brian pointed me to the inlineformsets documentation and I have not been happier. It really is easy now to build forms with inlines. This stuff has come a long way, and life will be much easier than it was before. Thanks for Django, Maeck --~--~-~--~~~---~--~~ 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: FormSet issue. Pre-populating from queryset
On Thu, 2008-12-11 at 20:55 -0800, maeck wrote: [...] > Now I can pass the fieldnames as values('parent') for now, It would be > easier if initial did not care if the _id is provided or not. > Or am I missing something else? What you're missing, or rather, assuming, is that querysets are ideal or intended to be used directly as initial data, and that isn't the case. Forms are separate pieces of code from models and shouldn't have to understand all the foibles of models (i.e. enforce loose coupling). If you want to pass in the results of a queryset directly, it's easy, but you do have to take care and pass in the right values by specifying the names of the fields. You know the models you are using, so writing down their field names isn't particularly onerous. It happens that values(), by default, returns "parent_id" for historical reasons. However, we set things up so that you an also ask it to return "parent" by specifying that in the fields list. So use that functionality. If you still think that's all just nit-picking and we should compromise to avoid the inconvenience, realise how bad it is: first you have to teach Form's initial data that when they expect "foo", it might really be spelt "foo_id". The form doesn't know this is a foreign key, it just knows it's a choice field. But since "_id" isn't the only possible suffix, now the form has to be able to handle any possible suffix -- "foo_blah", for example -- since the database column name can be specified in the model. So we're looking at all sorts of possible alternate names for the key. Thus, the form really needs to have reference to the full model to inspect all that. Now we're passing in models and tying the form to the model and it's starting to look like you really should be using ModelFormSet. Short version: it's just not worth all the complexity. We have ModelFormSets for standard model-> form conversions and an easy way to produce sequences of dictionaries if you have other situations where a FormSet is more useful. These things have a way of being much more complicated than they look on the surface. :-) Regards, Malcolm --~--~-~--~~~---~--~~ 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: FormSet issue. Pre-populating from queryset
On Thu, Dec 11, 2008 at 9:55 PM, maeckwrote: > Now I can pass the fieldnames as values('parent') for now, It would be > easier if initial did not care if the _id is provided or not. > Or am I missing something else? You shouldn't be using a regular formset. Django provides model formsets that know how to deal with this internally. Go read up on inline formsets which is a layer on top of model formsets. http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#inline-formsets -- Brian Rosner http://oebfare.com --~--~-~--~~~---~--~~ 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: FormSet issue. Pre-populating from queryset
Thanks Malcolm, Just figured out the values transformation on querysets myself. Nevertheless, in my experience there seems to be an issue with foreignkeys when using queryset values in combination with formsets. Values returns keys like 'parent_id', however formsets expect the fieldname as 'parent'. (I am using Django 1.0.2) Now I can pass the fieldnames as values('parent') for now, It would be easier if initial did not care if the _id is provided or not. Or am I missing something else? Maeck --~--~-~--~~~---~--~~ 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: FormSet issue. Pre-populating from queryset
On Thu, 2008-12-11 at 20:19 -0800, maeck wrote: > The example below is a snippet from a view where I use a form to show > 'Parent' and a formset to show its 'Children'. > If I get the children as a queryset and pass it on to the formsets > initial property, it errors out with: 'Parent' object is not iterable > > InlineFormSet = formset_factory(InlineForm) > data = Parent.objects.get(id = data_id) > form = ParentForm(instance=data) > inlinedata= Child.objects.filter(parent_id = data_id) > inlineform= InlineChildFormSet(initial=inlinedata) Initial for data should be a something that is, or at least behaves like, a sequence of dictionaries. A queryset is behaves like a sequence, but the elements of that sequence don't behave like dictionaries (they're objects -- in this case, Parent objects). So whilst, "for key in obj:" works when "obj" is a dictionary, it doesn't work when "obj" is a Django Model subclass (e.g. a Parent). Since the requirement is to pass in a list of dictionaries, using the values() method on your inlinedata call will get you a long way there. Regards, Malcolm --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---