On Jul 2, 6:53 pm, b14ck <[email protected]> wrote: > Hi everyone. > > I'm working on a mid-sized web project, and have run into a bit of a > dead-end trying to figure out how to make my custom manager method > `for_user` return a a proper Django QuerySet. > > First off, here's my: myproject/partylines/models.py source code which > contains the problem that I'm trying to figure > out:http://pastie.org/private/zrqc0wklg4nwpmu8oce8aq > > I'm attempting (in another part of my website) to render a form which > displays a dropdown of all Partyline objects that a given user owns. > My form looks like:http://pastie.org/private/mimiwo1vlafz5cxktn2gla > > As you can see, the form accepts a custom parameter (user), and should > modify the queryset to show *only* Partyline objects that the > specified user owns. In the original paste which contains my manager > code, you'll see that I've overridden the default 'objects' manager > with my custom manager that has a single method, `for_user`, that > returns a list of Partylines that a user owns. > > Basically, since my `for_user` manager method returns a list, and not > a QuerySet, the __init__ field in my `PartylineReportForm` class > doesn't work because it requires a QuerySet. > > What I'm trying to figure out is, how can I modify my `for_user` > manager method to return a QuerySet instead of a list? I've been > googling this for hours, and haven't seen any examples or found any > documentation. > > If anyone could help me out I would greatly appreciate it. > > Thanks so much!
The trouble is that you have used a Python list comprehension to filter the users in your manager, instead of using the ORM to translate into SQL commands to filter in the database. Not only is this incompatible with the modelform you want to use, it's also significantly less efficient - you will be getting every single User entry from the table, instantiating Django objects, then throwing them away. (In fact, because each call to is_owner does a separate call to user.get_profile, you'll be hitting the database multiple times for each User in your database). Instead, you need do this in a `filter()` call. I can't do the conversion myself because you haven't provided the code to your UserProfile and its is_creator method, but it looks like you're familiar with building up queries via Q objects, so you should be OK. -- DR. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to [email protected]. 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.

