Re: UserProfile and select_related

2009-08-03 Thread Malcolm Tredinnick

On Mon, 2009-08-03 at 08:10 -0700, lfrodrigues wrote:
> Isn't there any way to cache profile data on User side? 

This already happens when you call get_profile(). If you mean using a
mass query, then you have just asked if there is a select_related()
equivalent for reverse relations. For the answer to that, see my
previous mail in this thread.

> When are you
> planning on closing 7270?

When it is appropriate. The patch needs some fixing and it didn't make
the cut for 1.1. It's a good candidate for 1.2, but the answer is always
"when it's ready".

> 
> A lot of my code uses get_profile in templates and that is always a
> query. Isn't it possible to optimize this?

Everything is possible. Asking the same question three or four times
isn't the way to do it, however. It's been explained in this thread what
you can do at the moment. Querying UserProfile instead of User is a very
practical technique. Some quite large sites use it in that fashion (I'm
not at liberty to disclose names, so you can trust me or not).

Does this mean you can just pass around a bunch of User objects and not
have to do any code changes? No. But optimisation almost always involves
some code changes and passing around UserProfiles instead isn't
particularly illogical.

Regards,
Malcolm


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



Re: UserProfile and select_related

2009-08-03 Thread lfrodrigues

Isn't there any way to cache profile data on User side? When are you
planning on closing 7270?

A lot of my code uses get_profile in templates and that is always a
query. Isn't it possible to optimize this?

On Aug 3, 3:45 pm, lfrodrigues  wrote:
> Ok  Thanks
>
> On Aug 3, 12:12 pm, Malcolm Tredinnick 
> wrote:
>
> > On Mon, 2009-08-03 at 02:48 -0700, alant...@neei.uevora.pt wrote:
> > > Hello,
>
> > > I'm displaying lot of user profile picture with are on a user profile
> > > (i'm using contrib.auth). So for each picture I have a query for the
> > > user profile.
>
> > > I wanted to use User.objects.filter(stuff).select_related
> > > ('userprofile') but this has no effect. Any ideas how to prevent the
> > > +200 queries from happening?
>
> > The link between the two models runs from UserProfile -> User, not the
> > other way around. The select_related() call only follows forwards links,
> > so it will not traverse from User to UserProfile. It's not completely
> > trivial to add backwards-link following, particularly because there can
> > be multiple values for backwards links.
>
> > However, you can often turn this type of query around. Instead of
> > filtering User objects, filter Userprofile objects, where you can use
> > select_related(). Thus
>
> >         User.objects.filter(username="fred")
>
> > becomes
>
> >         UserProfile.objects.filter(user__username="fred").select_related()
>
> > The main trick here is that each condition needs the "user__" bit
> > prepended, so a little munging of filters -- either automatically or by
> > hand -- is required. You can still access the User instance attributes
> > via
>
> >         obj.user.username
>
> > where "obj" is now a UserProfile instance.
>
> > Regards,
> > Malcolm
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: UserProfile and select_related

2009-08-03 Thread lfrodrigues

Ok  Thanks

On Aug 3, 12:12 pm, Malcolm Tredinnick 
wrote:
> On Mon, 2009-08-03 at 02:48 -0700, alant...@neei.uevora.pt wrote:
> > Hello,
>
> > I'm displaying lot of user profile picture with are on a user profile
> > (i'm using contrib.auth). So for each picture I have a query for the
> > user profile.
>
> > I wanted to use User.objects.filter(stuff).select_related
> > ('userprofile') but this has no effect. Any ideas how to prevent the
> > +200 queries from happening?
>
> The link between the two models runs from UserProfile -> User, not the
> other way around. The select_related() call only follows forwards links,
> so it will not traverse from User to UserProfile. It's not completely
> trivial to add backwards-link following, particularly because there can
> be multiple values for backwards links.
>
> However, you can often turn this type of query around. Instead of
> filtering User objects, filter Userprofile objects, where you can use
> select_related(). Thus
>
>         User.objects.filter(username="fred")
>
> becomes
>
>         UserProfile.objects.filter(user__username="fred").select_related()
>
> The main trick here is that each condition needs the "user__" bit
> prepended, so a little munging of filters -- either automatically or by
> hand -- is required. You can still access the User instance attributes
> via
>
>         obj.user.username
>
> where "obj" is now a UserProfile instance.
>
> Regards,
> Malcolm
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: UserProfile and select_related

2009-08-03 Thread Alex Gaynor

On Mon, Aug 3, 2009 at 5:12 AM, Malcolm
Tredinnick wrote:
>
> On Mon, 2009-08-03 at 02:48 -0700, alant...@neei.uevora.pt wrote:
>> Hello,
>>
>> I'm displaying lot of user profile picture with are on a user profile
>> (i'm using contrib.auth). So for each picture I have a query for the
>> user profile.
>>
>> I wanted to use User.objects.filter(stuff).select_related
>> ('userprofile') but this has no effect. Any ideas how to prevent the
>> +200 queries from happening?
>
> The link between the two models runs from UserProfile -> User, not the
> other way around. The select_related() call only follows forwards links,
> so it will not traverse from User to UserProfile. It's not completely
> trivial to add backwards-link following, particularly because there can
> be multiple values for backwards links.
>
> However, you can often turn this type of query around. Instead of
> filtering User objects, filter Userprofile objects, where you can use
> select_related(). Thus
>
>        User.objects.filter(username="fred")
>
> becomes
>
>        UserProfile.objects.filter(user__username="fred").select_related()
>
> The main trick here is that each condition needs the "user__" bit
> prepended, so a little munging of filters -- either automatically or by
> hand -- is required. You can still access the User instance attributes
> via
>
>        obj.user.username
>
> where "obj" is now a UserProfile instance.
>
> Regards,
> Malcolm
>
>
> >
>

It's worth noting that the ability to do this is the subject of ticket
7270: http://code.djangoproject.com/ticket/7270

Alex

-- 
"I disapprove of what you say, but I will defend to the death your
right to say it." -- Voltaire
"The people's good is the highest law." -- Cicero
"Code can always be simpler than you think, but never as simple as you
want" -- Me

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



Re: UserProfile and select_related

2009-08-03 Thread Malcolm Tredinnick

On Mon, 2009-08-03 at 02:48 -0700, alant...@neei.uevora.pt wrote:
> Hello,
> 
> I'm displaying lot of user profile picture with are on a user profile
> (i'm using contrib.auth). So for each picture I have a query for the
> user profile.
> 
> I wanted to use User.objects.filter(stuff).select_related
> ('userprofile') but this has no effect. Any ideas how to prevent the
> +200 queries from happening?

The link between the two models runs from UserProfile -> User, not the
other way around. The select_related() call only follows forwards links,
so it will not traverse from User to UserProfile. It's not completely
trivial to add backwards-link following, particularly because there can
be multiple values for backwards links.

However, you can often turn this type of query around. Instead of
filtering User objects, filter Userprofile objects, where you can use
select_related(). Thus

User.objects.filter(username="fred")

becomes

UserProfile.objects.filter(user__username="fred").select_related()

The main trick here is that each condition needs the "user__" bit
prepended, so a little munging of filters -- either automatically or by
hand -- is required. You can still access the User instance attributes
via

obj.user.username

where "obj" is now a UserProfile instance.

Regards,
Malcolm


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



UserProfile and select_related

2009-08-03 Thread alant...@neei.uevora.pt

Hello,

I'm displaying lot of user profile picture with are on a user profile
(i'm using contrib.auth). So for each picture I have a query for the
user profile.

I wanted to use User.objects.filter(stuff).select_related
('userprofile') but this has no effect. Any ideas how to prevent the
+200 queries from happening?

Regards,

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