On Sat, 2009-08-08 at 22:00 -0700, Gleber wrote:
[...]
> When I override the get_query_set() adding extra fields, these fields
> aren't available if I execute:
> ModelB.objects.all().select_related('ref')

This is true.

> This is a bug? 

Not necessarily. It's intentional behaviour at the moment and it's
fairly tricky to make things work the way you want. *Particularly*
because you're using extra(). All bets are off once you start
introducing extra() into things, because that introduces opaque SQL
strings, so the necessary alias relabelling and the like that has to go
on to move query parts around isn't going to be able to work with that.

Worthwhile opening a ticket for this enhancement request. Don't be too
surprised if it's ultimately closed as wontfix, but I'd like to have a
think about it for a bit and see if the passing of a few months brings
up new ways to do this that I didn't think of the first couple of times
I worked on this problem.

> If not, how can I get the extra fields from ModelA
> through ModelB with only one query?

It might not be possible.

> I need this cause I have to display a list of 50+ ModelBs and i can't
> execute 50+ queries only for that..

Which you don't have to do. You can use normal select_related()
behaviour and then do an extra query to retrieve the necessary extra
fields and iterate through the queryset attaching the extra attributes
to the models. So it's one extra query.

You could even do this as a subclass of QuerySet as a specially named
method if you were doing it often enough that you wanted it wrapped up
neatly.

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

Reply via email to