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 3239488
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:58:33+00:00 2026-05-17T17:58:33+00:00

I am still a bit confused about the relation of Proxy models to their

  • 0

I am still a bit confused about the relation of Proxy models to their Superclasses in django. My question now is how do I get a instance of a Proxy model from an already retrieved instance of the Superclass?

So, lets say I have:

class Animal(models.Model):
   type = models.CharField(max_length=20)
   name = models.CharField(max_length=40)

class Dog(Animal):  
   class Meta:
       proxy = True

   def make_noise(self):  
       print "Woof Woof"  

Class Cat(Animal):  
   class Meta:
       proxy = True

   def make_noise(self):  
       print "Meow Meow"

animals = Animal.objects.all()
for animal in animals:
   if (animal.type == "cat"):
      animal_proxy = # make me a cat
   elif (animal.type == "dog"):
      animal_proxy = # make me a dog
   animal_proxy.make_noise()

OK. So.. What goes into “# make me a cat” that doesn’t require a query back to the database such as:

animal_proxy = Cat.objects.get(id=animal.id)

Is there a simple way to create an instance of Cat from an instance of Animal that I know is a cat?

  • 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. Editorial Team
    Editorial Team
    2026-05-17T17:58:34+00:00Added an answer on May 17, 2026 at 5:58 pm

    You are trying to implement persistence for an inheritance hierarchy. Using one concrete table and a type switch is a nice way to do this. However I think that your implementation, specifically:

    for animal in animals:
       if (animal.type == "cat"): 
          animal_proxy = # make me a cat
    

    is going against the grain of Django. The switching on type shouldn’t be extraneous to the proxy (or model) class.

    If I were you, I’d do the following:

    First, add a “type aware” manager to the proxy models. This will ensure that Dog.objects will always fetch Animal instances with type="dog" and Cat.objects will fetch Animal instances with type="cat".

    class TypeAwareManager(models.Manager):
        def __init__(self, type, *args, **kwargs):
            super(TypeAwareManager, self).__init__(*args, **kwargs)
            self.type = type
    
        def get_query_set(self):
            return super(TypeAwareManager, self).get_query_set().filter(
                  type = self.type)
    
    class Dog(Animal):
        objects = TypeAwareManager('dog')
        ...
    
    class Cat(Animal):
        objects = TypeAwareManager('cat')
        ...
    

    Second, fetch subclass instances separately. You can then combine them before operating on them. I’ve used itertools.chain to combine two Querysets.

    from itertools import chain
    q1 = Cat.objects.all() # [<Cat: Daisy [cat]>]
    
    q2 = Dog.objects.all() # [<Dog: Bruno [dog]>]
    
    for each in chain(q1, q2): 
        each.make_noise() 
    
    # Meow Meow
    # Woof Woof
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read few articles about table partioning but still I am bit confused
I am still a bit confused about something about FormsAuthenticationTicket and the actual cookie
Im new to Android development and Im still a bit confused about making the
I am used to enums in C, still a bit confused about how they
I am still a bit confused about the #import statement in Objective-C. I have
hello im still a student and im a bit confused about stacking and queuing
I've read about CFRunLoop but still a bit confused about it. I came a
I'm still a bit confused on models and saving data, perhaps someone could help
after reading the cookbook and various Q&A here I am still a bit confused
I've done some looking around, but I'm still confused a bit. I tried Crockford's

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.