title: Type casting in python
body: <p>I looked at similar question on SO but none of them answer my problem. For Ex. <a href="https://stackoverflow.com/questions/1120156/how-do-you-cast-an-instance-to-a-derived-class">How do you cast an instance to a derived class?</a> . But the answer doesn't seem to be what I want.</p>
  
  <p>Here is my situation. I have a class structure like in Django</p>
  
  <pre><code>class Base:
       ...some stuff...
  
  class Derived(Base):
       ...some more stuff...
  </code></pre>
  
  <p>Now when I make some queries in Django, I always get objects of Base class. </p>
  
  <pre><code> baseobj = get_object_or_404(Base, id = sid)
  </code></pre>
  
  <p>At runtime I can also encounter "Derived" objects which have some extra properties. I'm able to figure out if an object is Base or derived(there is sufficient info in Base class object). But how should I access those extra fields which are present only in Derived class. I'm not able to downcast "Base" -> "Derived". How should I handle it?</p>
  
  <p>EDIT:</p>
  
  <p>I figured out the problem. Django stores "additional properties" of Derived class in a separate table. Hence problem arose due to this line of code.</p>
  
  <pre><code>baseobj = get_object_or_404(Base, id = sid)
  </code></pre>
  
  <p>baseobj will always be of Base class and will not have any properties of Derived class. I have to make an additional query to get the Derived class object. </p>
  
  <pre><code>baseobj = get_object_or_404(Base, id = sid)
  if baseobj.isDerivedType:
        derivedobj = get_object_or_404(Derived, id = sid)
  </code></pre>