Categories > Coding > Python >

A confusing situation in python

UlaDeia

Senior Developer

Posts: 14

Threads: 9

Joined: Jul, 2022

Reputation: 2

Posted

Okay so its abaout type casting in python. i was reading an article about this here and eventually grow a doubt.

Here's my predicament. I have a class structure similar to Django.

class Base:
     ...some stuff...

class Derived(Base):
     ...some more stuff...

Now, whenever I run a query in Django, I always receive objects of the Base type.

 

baseobj = get_object_or_404(Base, id = sid)

At runtime, I may find "Derived" objects with additional attributes. I can tell whether an item is base or derived (there is sufficient info in Base class object). But how can I get to those extra fields that are only available in the Derived class? I can't downcast "Base" to "Derived". How should I deal with it?

  • 0

Users viewing this thread:

( Members: 0, Guests: 1, Total: 1 )