I've been looking through the ModelForms code and can't really find a way to fix this.
I changed the view code to this: print form.category.queryset form.category.queryset = Category.objects.filter(blog__exact=request.user.author_set.all() [0].blog) print form.category.queryset And my console output is this: [<Category: another category>, <Category: my cat>, <Category: random cat>] [<Category: random cat>] So the first line is all of the categories and the second line is the filtered queryset. However, the html form still shows all of the categories... On Jun 17, 2:06 am, "Jonas Oberschweiber" <[EMAIL PROTECTED]> wrote: > Hmm, yeah, I guess it should... I don't really know much about the > inner workings of ModelForms, but you could try inserting print > statements or something the like directly in the django code for > testing purposes. Or just look at the django code. That almost always > solves the problems I have. > > On Tue, Jun 17, 2008 at 8:03 AM, emont88 <[EMAIL PROTECTED]> wrote: > > > Ok, that makes sense, but I'm still getting weird behavior. > > Right after I call limit_categories() in my view, I inserted the print > > statement: "print form.category.queryset" so that I can see the > > contents of the queryset printed to the console. The result is that > > the console shows the correct thing - only the categories that belong > > to the current blog. However, the form itself still somehow displays > > all of the categories, or the categories that belong to the previous > > user's blog if I just logged out and logged in. > > If form.category.queryset is showing the right thing when printed to > > the console, shouldn't that be exactly what ends up in the html form? > > > On Jun 17, 1:37 am, "Jonas Oberschweiber" <[EMAIL PROTECTED]> > > wrote: > >> AFAIK querysets are always cached. > > >> On Tue, Jun 17, 2008 at 7:06 AM, emont88 <[EMAIL PROTECTED]> wrote: > > >> > I'm running into an interesting problem with ModelForms. > >> > I have a blogging app that handles multiple blogs, which makes things > >> > tricky with categories, because each category needs to belong to a > >> > specific blog, so an author editing blog1 should not be allowed to > >> > post an Entry to a Category from blog2. I am using ModelForms to > >> > generate the forms for posting a new Entry, which makes things pretty > >> > straightforward except for the Category foreignkey. I wrote a method > >> > "limit_categories" in my EntryForm class, which I call from the view > >> > after creating the form. This method takes in a Blog object, and > >> > limits the queryset for the Category foreignkey to categories that > >> > belong to that Blog. All seems good at this point. > >> > When I actually access the form though, the behavior is pretty > >> > random. Sometimes, it will not filter the categories at all. Other > >> > times, if I logout and login as another user, it will retain the > >> > category choices from the previous user. After a couple of refreshes > >> > though, it eventually works. > >> > I don't think I have any type of caching enabled, so I'm not sure > >> > what's going on here. > > >> > Here is some of my code, hopefully enough to figure things out: > > >> > class EntryForm(forms.ModelForm): > >> > class Meta: > >> > model = Entry > >> > fields = ('title', 'slug', 'pub_date', 'category', 'body', > >> > 'excerpt') > >> > # Must define category explicitly so that limit_categories() can > >> > access it > >> > category = forms.ModelChoiceField(queryset=Category.objects.all()) > > >> > def limit_categories(self, blog): > >> > # Limits the category choices to those belonging to the > >> > current blog > >> > print "Limiting categories" > > >> > self.category._set_queryset(Category.objects.filter(blog__exact=blog)) > > >> > @login_required > >> > def admin_entry_edit(request, entry_id): > >> > entry = get_object_or_404(Entry, id__exact=entry_id) > >> > if request.method == 'POST': > >> > form = EntryForm(request.POST, instance=entry) > >> > if form.is_valid(): > >> > form.save() > >> > request.user.message_set.create(message="Entry saved > >> > successfully.") > >> > return HttpResponseRedirect('../') > >> > else: > >> > form = EntryForm(instance=entry) > >> > form.limit_categories(request.user.author_set.all()[0].blog) > >> > context = {'form': form} > >> > return render_to_response('blogs/admin/edit.html', context, > >> > context_instance=RequestContext(request)) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---