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

  • Home
  • SEARCH
  • 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 603949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:59:10+00:00 2026-05-13T16:59:10+00:00

Basically I have a Base class called Program. I then have more specific program

  • 0

Basically I have a Base class called “Program”. I then have more specific program model types that use Program as a base class. For 99% of my needs, I don’t care whether or not a Program is one of the specific child types. Of course there’s that 1% of the time that I do want to know if it’s one of the children.

The problem is that if I have let’s say, a SwimProgram model and a CampProgram model using Program as their base, that it’s problematic to find out what they are without a bunch of try/except blocks. What I want is something like the following:

program = models.Program.objects.get(id=15)
if program.swimprogram:
    ## do stuff
elif program.campprogram:
    ## do stuff
else:
    ## do other stuff

Of course this throws DoesNotExist exceptions. I could either use try/excepts which are uglier, or I could have Program have a ‘type’ field that the children set on save. Both are doable, but I’m curious if anyone has any better methods.

  • 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-13T16:59:10+00:00Added an answer on May 13, 2026 at 4:59 pm

    Have you tried hasattr()? Something like this:

    if hasattr(program, 'swimprogram'):
        # ...
    elif hasattr(program, 'campprogram'):
        # ...
    

    If you are unsure about this approach, try it out in a simple test app first. Here are two simple models that should show if it will work for you and the version of django that you are using (tested in django-1.1.1).

    class Archive(models.Model):
        pub_date = models.DateField()
    
        def __unicode__(self):
            return "Archive: %s" % self.pub_date
    
    class ArchiveB(Archive):
        def __unicode__(self):
            return "ArchiveB: %s" % self.pub_date
    

    And then giving it a spin in the shell:

    > a_id = Archive.objects.create(pub_date="2010-10-10").id
    > b_id = ArchiveB.objects.create(pub_date="2011-11-11").id
    > a = Archive.objects.get(id=a_id)
    > b = Archive.objects.get(id=b_id)
    > (a, b) # they both look like archive objects
    (<Archive: Archive: 2010-10-10>, <Archive: Archive: 2011-11-11>)
    > hasattr(a, 'archiveb')
    False
    > hasattr(b, 'archiveb') # but only one has access to an ArchiveB
    True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I basically have a program that filters records from one excel file to another
Basically, I have a pure virtual class Base, and a concrete class Derived which
Basically I have a simple base class: public abstract class BasePoco { public virtual
I have a simple base entity type called EntityBase that implements IEquatable<EntityBase>. I've used
I have a model called Notifications, which basically acts as a join table between
I have a custom class that is basically a SortedList with a few extra
I have a base class that has an abstract getType() method. I want subclasses
I have a base class that has a method that gets executed by derived
I have a base class called Object. PhysicsObject inherits from Object. Ball inherits from
I basically have 7 select statements that I need to have the results output

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.