Thanks, this was exactly what I was looking for, here's the code I used: form.fields["rate"].queryset = Rate.objects.filter(company_id=the_company.id)
Although with my own fields, and it worked perfectly. Something else that other users may use more than this type would be something like this: form.fields["rate"].queryset = Rate.objects.filter(owner_id=req.user) The request object cannot be accessed when building the ModelForm class, for setting a filter such as this. Although this filter can be easily done a different way. On Jan 4, 2:45 am, <michael.pimmer....@boehringer-ingelheim.com> wrote: > > I'm not sure if this will > > work, as the ModelForm's QuerySet for the choiceField cannot be > changed dynamically during runtime, or can it? > > You can change the queryset directly in the view: > > http://stackoverflow.com/questions/291945/how-do-i-filter-foreignkey-... > in-a-django-modelform > > Altough having this stored in the model would be more elegant in your case, > perhaps anyone else has knows more.. > > Michael > > > > > > > > -----Original Message----- > From: django-users@googlegroups.com on behalf of Kevin > Sent: Wed 1/4/2012 8:07 AM > To: Django users > Subject: Limiting choices for ModelForm's ForeignKey/ManyToMany > > Hello, > > I have many models which have a foreign key to a main model. Eg: > > class MainModel: > .. > .. > > class VariousModels: > main = ForeignKey(MainModel) > .. > .. > > Now, these VariousModels sometimes have links to each other in the > form of ForeignKey or ManyToMany. I need to limit these ForeignKey > and ManyToMany in various ModelForms to only display other > VariousModels which share the same MainModel in common. Here is what > I tried to do and it didn't work: > > item = models.ForeignKey(Item, limit_choices_to={'main__pk':main}, > blank=True, null=True) > > I attempted different versions of this on the actual model. I am now > looking into how I can perform this using a ModelForm instead of > placing the limits on the actual model. I'm not sure if this will > work, as the ModelForm's QuerySet for the choiceField cannot be > changed dynamically during runtime, or can it? > > Basically, I am creating a multi-user/multi-section application. The > main model described above is a section which a user creates and > manages. The user should only see choices in the forms for objects > associated with the current section they are editing. > > Perhaps I am going about this entirely wrong and this should be > implemented in a different form, such as a permission. > > Any ideas on how this can be done would be very helpful, 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 > django-users+unsubscr...@googlegroups.com. > For more options, visit this group > athttp://groups.google.com/group/django-users?hl=en. > > > > winmail.dat > 5KViewDownload -- 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.