On Jun 23, 8:24 pm, Rich Jones <miser...@gmail.com> wrote:
> Other than the convenience of writing them, I can't seem to find any
> advantages of using model inheritance.

Really ???


> Please allow me to explain the trouble I am having with an example.
> Suppose,
>
> class Post(models.Model):
>     title = models.CharField(max_length=200)
>     user_owner_id = models.ForeignKey(User)
>
>     def __unicode__(self):
>         return self.title
>
> class BetterPost(Post):
>     description = models.CharField(max_length=200)
>
>     def __unicode__(self):
>         return self.title

You don't need to redifine __unicode__ here.

> Now, in the template:
>
> {% with profile.user.post_set.select_related as posts %}

For what you're doing in the template and given your models, the
select_related is totally useless.

>     {% for post in posts %}
>         {{post}}
>     {% empty %}
>         No posts!
>     {% endfor %}
> {% endwith %}
>
> {% with profile.user.betterpost_set.select_related as posts %}
>     {% for post in posts %}
>         {{post}}
>     {% empty %}
>         No posts!
>     {% endfor %}
> {% endwith %}
>
> If there are 2 'Posts' and 1 'BetterPost',

Belonging to the same user ???

> the template code will
> print all of the posts in the first loop, then none in the second loop
> set.

If so, please inspect your data, as there's obviously something wrong.
With your data, I mean.

> So why would I use model inheritance if this is the kind of behavior I
> can expect? If I can't get a set and can't access the fields, why
> wouldn't I just copy the fields into BetterPost rather than extending
> Post?

If that was the case, then model inheritance would indeed be a plain
waste of time. Now, just a simple question: do you really think anyone
would have wasted time implementing model inheritance, testing it, and
documenting it if that was all you could "expect" from it ?

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

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