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 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

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is due to String interning. Java (The JVM) keeps… May 16, 2026 at 3:46 pm
  • Editorial Team
    Editorial Team added an answer I would expect that you are persisting the configuration data… May 16, 2026 at 3:46 pm
  • Editorial Team
    Editorial Team added an answer You're using a feature called collection initializers which was added… May 16, 2026 at 3:46 pm

Trending Tags

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

Top Members

Related Questions

Basically, I have a pure virtual class Base, and a concrete class Derived which
I have a container class that has a generic parameter which is constrained to
I have a number of data classes, which share an abstract base class so
We're using SQLAlchemy declarative base and I have a method that I want isolate
At our company, we use a base ant file that is included by everyone
I basically have this markup coming from my JSP. I add class in each
I have an issue with some inheritance I'm doing. Basically, I have an abstract
I have several forms in a C# application. I use Visual Studio 2010 Beta,
I have a follwing class structure: public abstract class AbstractFoo { public virtual void
I have a major problem. We have a asp.net application that has this report

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.