Thursday, June 23, 2011

Re: What are the advantages of Model inheritance?

I might be wrong here, but I'm fairly sure that the below example you pasted is invalid code (i.e. you need to add a proxy _meta??).

My apologies if I'm wrong though, as I have never encountered a need to do this before, and just going off vague memory.

Cal

On 23/06/2011 19:24, Rich Jones wrote:
Other than the convenience of writing them, I can't seem to find any advantages of using model inheritance.  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  Now, in the template:  {% with profile.user.post_set.select_related as posts %}     {% 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', the template code will print all of the posts in the first loop, then none in the second loop set.  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?  Thank you for reading! R  

No comments:

Post a Comment