Instead of leaping right into a proposed solution, can you explain the
problem you're seeing, please?
Regards,
Malcolm
So it's not about "copying" -- taking one Python object and creating a
similar, but independent one -- at all. You're talking about how far
down the relation chain we descend when retrieving data.
Bob Thomas has already point out one ticket and there are some others
opened regarding pulling related models in via a values() call
(searching for tickets about values() will reveal a bunch of different
directions and proposals).
Your patch isn't particularly neat (examining the string representation
of the output of type() to determine a class when isinstance() exists,
for example). It also looks like it will fail for infinitely recursive
structures (which exist in practical situations).
Utlimately, though, I think this situation is solved by allowing
select_related() to work with values() -- ticket #5768 is one reference
to that. It's not a trivial problem to solve, but we'll fix it one day.
Multi-valued relations, in particular, require care to make them work
efficiently.
Regards,
Malcolm
They've been open for two years because nobody has fixed them yet and we
think it's worthwhile doing (plus they're not entirely trivial to fix,
so that cuts down the number of people willing to put in the effort). In
the interim we've closed, you know, a *few thousand* other tickets, so
progress has definitely been made.
I'll also note that parts of #5768 have been fixed, e.g., in r7230. Lots
of those bigger items are multi-part projects that get fixed in a few
stages.
You could work on those tickets if you want to help. I've pointed out
the difficulties in comment 4 on #5768. Whether we restrict values() to
only allowing one multi-valued relation or, preferably, constructing the
correct SQL for querying many multi-valued relations (making sure we
only return 1 + n1 +n2 rows, not n1 * n2 rows, in the notation in that
comment). The latter situation is best, but hard to implement.
So start working on that if you want this solved. We aren't going to
commit a hack to work around something when the real problem is known.
If you get stuck, ask as many questions as you like on this list.
Regards,
Malcolm