From: Malcolm Tredinnick <malc...@pointy-stick.com>
Date: Fri, 18 Apr 2008 13:44:57 +1000
Local: Fri, Apr 18 2008 1:44 pm
Subject: Re: Feel free to test queryset-refactor branch
On Thu, 2008-04-17 at 14:53 -0700, Justin Fagnani wrote: [...] > The first thing is that there seems to be no way to tell if an That's a reasonable way to do it. > instance of a parent class has a child instance without trying the > child reference and catching the DoesNotExist exception. For a class > with multiple subclasses, this is a cumbersome, so I've been adding a > _type field to parent classes that gets set in save() of the > subclasses. Another way is to have an extra table that maps object id and content > Is there a better way to do this, or is this something that could be That's right. There's no way to tell if something's going to be > included? I know there's no way to determine whether or not a class > will be subclassed in the future, so I wouldn't be surprised if the > answer is no. subclassed and we don't want to change any third-party database tables (a design feature is that you *must* be able to subclass third-party models transparently). Also, it's quite fiddly to keep such fields up to date if you think about the manipulation required for A subclassed by B subclassed by C. You end up with type fields everywhere. > But maybe there should be a documented pattern. I'll add something. It seems kind of obvious, though, and given that it isn't a particularly common pattern to query the parents and descend to the children (if you care about the differences, you'll usually be querying the children directly; if you care about the common stuff, it's all on the parent), I'd kind of hope people needing this already had the skills to connect A to B. A couple of sentences won't confuse things too much, though. We can fix > The odd part is what happens with the child reference. parent.child Hmm, hadn't noticed that, although it's not too surprising. My first > obviously works as expected, and returns either an instance of Child > or raises DoesNotExist. But for an instance of Child, .child always > returns a reference to itself, so that c.child == c is always True. > This makes sense on one hand, because c is also an instance of Parent, > but on the other, Child doesn't have a subclass, so should .child be > None? reaction is "well, don't do that." It's quite possibly fiddly to fix, since reverse relations (which is It's probably overkill to add in lots of extra processing to avoid the The hard cases are always multi-layer hierarchies (trees of inheritance, > I haven't actually encountered this in any real life situation, Indeed (hopefully). > because it's hard to end up with collection in Django where you have a > mix of parent and child instances, so maybe it'll never be a problem. We're not actually just making this up as we go along and I suspect > One additional thing is that in one case, I know which subclasses I'm Maybe one day. > interested in, and it'd be great to have a way to specify that a > queryset should return polymorphic results by specifying the > subclasses for the join. Something like: > Parent.objects.all().select_subclasses('Child1','Child2') It's not significantly less faster to just create two querysets in this Combining disparate sets of output columns into the appropriate models Regards, -- You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||