On Sun, 2007-07-01 at 12:59 +0000, benji wrote:
> My models <http://dpaste.com/13334/> contain a m2m relationship
> between a Profile and a GamePlatform.
> 
> I want a view to output the list of entire GamePlatforms, but flag
> those that the Profile has a relationship with.
> 
> I can do:
> for platform in GamePlatform.objects.all():
> 
> but then how do I mark the appropriate platforms?
> 
> I'm guessing I need a custom manager, but how do I access the
> currently logged in user's Profile and mark the QuerySet as necessary?

You're thinking along the right lines here -- a custom method is going
to be needed and since it will act on the whole set of GamePlatform
instances, putting it into a custom manager might be convenient (but not
compulsory).

There are two options as far as passing in the "current" user. One is to
use threadlocal storage (there's a middleware for this -- search the
wiki) and put it in there. Some people like that.

The other option is just to pass in the user when you call the method to
do this computation. I tend to prefer that style, since it makes
everything explicit -- there's no invisible side-effect from something
in the environment, which can make debugging easier -- but, like I said,
different people have different preferences and neither alternative make
global warming worse or anything.

As to the mechanics of how to actually build this collection of objects,
here's one approach (there are others): retrieve the list of
GamePlatform instances for the given user (list1) and retrieve the list
of all GamePlatform instances (list2). Turn list2 into a dictionary
based on something like the name. Then run through list1 and for each
item, add an extra attribute to the object in list2 to say "the user has
this platform". That will take linear time that is proportional to
len(list1) + len(list2).

Regards,
Malcolm


-- 
The only substitute for good manners is fast reflexes. 
http://www.pointy-stick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to