Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 76585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:41:47+00:00 2026-05-10T20:41:47+00:00

(Django 1.x, Python 2.6.x) I have models to the tune of: class Animal(models.Model): pass

  • 0

(Django 1.x, Python 2.6.x)

I have models to the tune of:

class Animal(models.Model):   pass  class Cat(Animal):   def __unicode__(self):     return 'This is a cat'  class Dog(Animal):   def __unicode__(self):     return 'This is a dog'  class AnimalHome(models.Model):   animal = models.ForeignKey(Animal) 

I have instantiated no Animals, because this is supposed to be a virtual class. I have instantiated Cats and Dogs, but in the Admin Page for AnimalHome, my choices for Animals are displayed as ‘Animal object’ (the default __unicode__(), I guess) as opposed to the __unicode__ I have defined for the two subclasses. Help.


The abstract base class issue is a red herring wrt to this question, I think. Even if Animal was not supposed to be abstract, I still have the problem that, for some reason, since the ForeignKey is defined on Animal and not one of its subclasses, the superclass method is being called instead of the subclass. In OO programming when you call object.method() you’re supposed to get the lowest-subclass’s implementation, and you have to do extra work to get any superclass’s implementation. So why is it that having __unicode__ defined on the subclasses is not sufficient for — actually the problem might be that __unicode__ is not getting called at all because introspection on the Animal class reveals that it’s not defined. So maybe if I define __unicode__ for Animal and have it call subclasses’ __unicode__ I could get the desired effect.


Okay, I think that I understand the ORM issues. Both these answers have helped me understand this, thank you. While experimenting with this, I discovered that when Django saves a subclassed model, it does two things: (1) it creates a row for the subclassed object in the superclass’s table, and (2) it makes the PK in the subclass table identical to the PK assigned in the superclass table. This PK in the subclass table is named superclass_ptr. Based upon this I’ve concocted the following. I’d appreciate feedback.

Class Animal(models.Model)   def __unicode__(self):     if Dog.objects.filter(pk=self.pk).count() > 0:       return unicode(Dog.objects.get(pk=self.pk))     elif Cat.objects.filter(pk=self.pk).count() > 0:       return unicode(Cat.objects.get(pk=self.pk))     else:       return 'An Animal!' 

It seems that Lawrence is most on-point wrt this question. Cat and Dog will have disjoint PK sets (and any subclass of Animal will have a PK identical to the record of its superclass), but unfortunately Django does not perform any work behind the scenes a la: ‘I’m an Animal. I know Animals have subclasses Dog and Cat. Specifically, I’m Animal number 3, and furthermore I just checked and there’s a Cat number 3 too. That means that I’m actually Cat number 3’. Even though this seems entirely possible and very reasonable (since a Cat won’t do anything an Animal couldn’t do itself) using Python’s introspection. Thank you all.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T20:41:48+00:00Added an answer on May 10, 2026 at 8:41 pm

    ForeignKey(Animal) is just that, a foreign key reference to a row in the Animal table. There’s nothing in the underlying SQL schema that indicates that the table is being used as a superclass, so you get back an Animal object.

    To work around this:

    First, you want the base class to be non-abstract. This is necessary for the ForeignKey anyway, and also ensures that Dog and Cat will have disjunct primary key sets.

    Now, Django implements inheritance using a OneToOneField. Because of this, an instance of a base class that has a subclass instance gets a reference to that instance, named appropriately. This means you can do:

    class Animal(models.Model):     def __unicode__(self):         if hasattr(self, 'dog'):             return self.dog.__unicode__()         elif hasattr(self, 'cat'):             return self.cat.__unicode__()         else:             return 'Animal' 

    This also answers your question to Ber about a unicode() that’s dependent on other subclass attributes. You’re actually calling the appropriate method on the subclass instance now.

    Now, this does suggest that, since Django’s already looking for subclass instances behind the scenes, the code could just go all the way and return a Cat or Dog instance instead of an Animal. You’ll have to take up that question with the devs. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 65k
  • Answers 65k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Without a doubt, blind trust of the client. In a… May 11, 2026 at 11:11 am
  • added an answer Negative zero is a useful concept for numerical computing and… May 11, 2026 at 11:11 am
  • added an answer You're using Java 5, but Properties.load(Reader) was only introduced in… May 11, 2026 at 11:11 am

Related Questions

(Django 1.x, Python 2.6.x) I have models to the tune of: class Animal(models.Model): pass
In Django 1.0, what is the best way to catch and show an error
I'm running Django 1.0 and I'm close to deploying my app. As such, I'll
Given a situation in Django 1.0 where you have extra data on a Many-to-Many
Does anyone know of a Django 1.0 + postgresql + apache + mod_python VMware
I have updated to latest Django version 1.0.2 after uninstalling my old Django version.But
Django view points to a function, which can be a problem if you want
Django comes with CSRF protection middleware , which generates a unique per-session token for
In Django's template language, you can use {% url [viewname] [args] %} to generate
Does Django have any template tags to generate common HTML markup? For example, I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.