Re: Filtering foreignkey results with newforms form_for_model
Thank you James, for both the solution and the advice. I really liked forms_for_model as a crutch while I got up to speed on newforms, but like most crutches, I'm finding I may spend more time working against it. Still, a nifty thing for a simple, straightforward form. On Jul 6, 3:12 pm, "James Bennett" <[EMAIL PROTECTED]> wrote: > On 7/6/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > > EventFormClass.base_fields['club'].widget = > > widgets.Select(choices=Club.objects.filter(approved=True)) > > > But that throws > > TypeError at /clubs/events/ > > unpack non-sequence > > The 'choices' argument should look like this: > > ((1, 'Foo'), > (2, 'Bar'), > (3, 'Baz'), > (4, 'Quux')) > > Instead, it's going to look like this: > > [, > , > , > ] > > So when the form goes to look "inside" each item to pull out the id > and string value of the choice, it finds that it can't -- it was > expecting a tuple or a list, and got a model instance instead. > > What you want is something like > > EventFormClass.base_fields['club'].widget = > widgets.Select(choices=[(club.id, str(club)) for club in > Club.objects.filter(approved=True)] > > Also, it's often a good idea to *not* try to shoehorn things into > form_form_model... usually it's quicker to write a custom form which > does what you want than to add/remove things from form_for_model. > > -- > "Bureaucrat Conrad, you are technically correct -- the best kind of correct." --~--~-~--~~~---~--~~ 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: Filtering foreignkey results with newforms form_for_model
On 7/6/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > EventFormClass.base_fields['club'].widget = > widgets.Select(choices=Club.objects.filter(approved=True)) > > But that throws > TypeError at /clubs/events/ > unpack non-sequence The 'choices' argument should look like this: ((1, 'Foo'), (2, 'Bar'), (3, 'Baz'), (4, 'Quux')) Instead, it's going to look like this: [, , , ] So when the form goes to look "inside" each item to pull out the id and string value of the choice, it finds that it can't -- it was expecting a tuple or a list, and got a model instance instead. What you want is something like EventFormClass.base_fields['club'].widget = widgets.Select(choices=[(club.id, str(club)) for club in Club.objects.filter(approved=True)] Also, it's often a good idea to *not* try to shoehorn things into form_form_model... usually it's quicker to write a custom form which does what you want than to add/remove things from form_for_model. -- "Bureaucrat Conrad, you are technically correct -- the best kind of correct." --~--~-~--~~~---~--~~ 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: Filtering foreignkey results with newforms form_for_model
I'm still struggling with this... came up with EventFormClass.base_fields['club'].widget = widgets.Select(choices=Club.objects.filter(approved=True)) But that throws TypeError at /clubs/events/ unpack non-sequence Any ideas? On Jul 5, 2:13 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > I have an "Event" model that has a foreignkey relationship with > "Club", and I'm using form_for_model to create the form. This spits > out all "Club" entries in a dropdown. > > The trick is, "Club" has a boolean field for "approved", and I want to > filter according to that. > > So, is there some way to filter it? At the newform level. Should I get > 'club.objects.filter(approved=true)' and pass that to form somehow > (maybe form.club = approvedclubs)? > > Thanks. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Filtering foreignkey results with newforms form_for_model
I have an "Event" model that has a foreignkey relationship with "Club", and I'm using form_for_model to create the form. This spits out all "Club" entries in a dropdown. The trick is, "Club" has a boolean field for "approved", and I want to filter according to that. So, is there some way to filter it? At the newform level. Should I get 'club.objects.filter(approved=true)' and pass that to form somehow (maybe form.club = approvedclubs)? Thanks. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---