I think you're right, Rich, that when you query on a table, it'll only grab
the results for that table.  So when you do user.post_set.all(), it only
creates Post objects, because it only pulls data from that one table.  When
you do user.betterpost_set.all(), the reason you don't see anything is that
that line of code raises an error, because user doesn't have a
"betterpost_set" attribute.  The templating language hides the error for
you, so you just get no output.

So, yeah, if you want real polymorphic inheritance, you aren't going to get
it with django, as far as I know.  And in that regard, you're probably
better off not using inheritance so that you won't expect those kinds of
features.  Abstract inheritance has its benefits in code simplification, and
I can imagine that multi-table inheritance also has some uses, but they are
not the ones that you typically expect from inheritance in python.

In case you aren't aware, to actually get at the subclass instance from a
superclass instance in django, you have to do something like this:

try:
    bp = post.betterpost
    # do stuff with bp
except BetterPost.DoesNotExist:
    pass

To get the behavior you want in that template, you need to manually create
the list of posts in the python code, doing something like the following:

posts_queryset = user.post_set.all()
posts = []
for post in posts_queryset:
    try:
        bp = post.betterpost
        posts.append(bp)
    except BetterPost.DoesNotExist:
        posts.append(post)
context['posts'] = posts

You can imagine that if you have several subclasses of Post this could get
pretty annoying.  I'd probably write a separate method to return the most
specific instance of an object that I could.

On Thu, Jun 23, 2011 at 5:54 PM, Rich Jones <miser...@gmail.com> wrote:

> Even ignoring the select_related then - the fact is that
>
> user.post_set works and user.betterpost_set won't work, seemingly as
> the result of using model inheritance.
>
> Perhaps I could get all of the posts (post and betterposts) and then
> filter by time? Does that make sense / is there a way to do that in
> the template?
>
> R
>
> On Jun 23, 5:40 pm, Micky Hulse <rgmi...@gmail.com> wrote:
> > On Thu, Jun 23, 2011 at 2:17 PM, bruno desthuilliers
> >
> > <bruno.desthuilli...@gmail.com> wrote:
> > > FWIW, I have been using model inheritance - abstract and concrete - in
> > > half a dozen projects, with about 30 model subclasses in one of them,
> > > and it JustWorks(tm).
> >
> > That sounds pretty sweet! :D
> >
> > I would love to see some examples.
> >
> > I have been using abstract models for some of my projects... I find
> > them extremely useful:
> >
> > https://gist.github.com/1043698
> >
> > It works for me... Of course, I am still a noobie to Python/Django, so
> > may the feedback be kind. :: crosses fingers :: :D
>
> --
> 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.
>
>

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