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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:55:59+00:00 2026-05-28T04:55:59+00:00

I have: class Foo(models.Model): pass class Bar(Foo): pass class Corn(Foo): pass # Now I

  • 0

I have:

class Foo(models.Model):
    pass

class Bar(Foo):
    pass

class Corn(Foo):
    pass

# Now I have these objects in the database:
john = Corn()
mary = Corn()
joe = Bar()
grace = Corn()
randy = Bar()

In Django, I use this to get a list of Foo objects:

foos = Foo.objects.all()

Now, how do I check if an object in foos list above is a Bar or a Corn?

for x in foos:
    print x.__class__.__name__ # returns Foo
  • 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-28T04:56:00+00:00Added an answer on May 28, 2026 at 4:56 am

    Model inheritance won’t automatically return the subclass instances — if you ask the ORM for Foos, it will give you Foos. You need to ask if there is a Bar object with the same primary key:

    for x in foos:
       Bar.objects.filter(pk=x.pk).exists() # True if it's a Bar, too
       Corn.objects.filter(pk=x.pk).exists() # True if it's a Corn, too
    

    You can also use the model name (in lowercase) as an attribute, which will do the lookup for you, but it’s not guaranteed to succeed (obviously), so you need to guard against DoesNotExist exceptions:

    for x in foos:
        try:
            x.bar
            # it's a bar
        except Bar.DoesNotExist:
            # it's not a bar
            pass
        try:
            x.corn
            # it's a corn
        except Corn.DoesNotExist:
            # it's not a corn
            pass
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following models: class Foo(models.Model): field1 = models.IntegerField() ... class Bar(models.Model): field1
I have two models: class Foo(models.Model): foo_field = ... class Bar(models.Model): foo = models.ForeignKey(Foo,
I have 2 django models: class Foo(models.Model): baz = models.CharField() class Bar(models.Model); foo =
I have an already existing app with alot of database entries . class Foo(models.Model):
I have a such model named Foo: class Foo(models.Model): name = models.CharField() entry =
I have the following model and instance: class Bashable(models.Model): name = models.CharField(max_length=100) >>> foo
Suppose I have class Foo(db.Model): bar = db.ReferenceProperty(Bar) foo = Foo.all().get() Is there a
Let's say I have a model like this class Foo(db.Model): id = db.StringProperty() bar
I have a model with nested attributes : class Foo < ActiveRecord::Base has_many :bar
I have class Foo(): function bar(): pass function foobar(): pass Rather than executing each

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.