12👍
Try to use the checkingaccount
and savingsaccount
attributes. The one it is will not blow up.
11👍
You could use isinstance(account, SavingsAccount)
, but is generally preferred to avoid it and use duck type inference by looking at the object’s attributes, and see if it quacks like a subclass.
To see if an object has an attribute, you use the aptly named hasattr
built-in function or use getattr
and check for the raising of an AttributeError exception.
- Multiple form classes in django generic (class) views
- How to append pages of data using jQuery and Django pagination?
- Django Test Client post() returns 302 despite error on view's post()
- How to download a filefield file in django view
- Get a list of python packages used by a Django Project
0👍
Add a GetAccountType() method to your Checking and Savings accounts, when you get the object back from BankAccount.objects.get() then call that, if everything that derives from BankAccount has that method then you’ll be fine.
- No Such Column Error in Django App After South Migration
- Django: how to change values for nullbooleanfield in a modelform?
- Django admin not serving static files?
- Django custom for complex Func (sql function)
- How to set up database for Django app on Heroku?
0👍
After some more searching I found solutions akin to this:
Django multi-table inheritance, how to know which is the child class of a model?
Basically, there’s no elegant solution for this. You have to do a bunch of try-except statements and force django to use the class you want.
- Django templates stripping spaces?
- RuntimeError: Never call result.get() within a task Celery
- How should I represent a bit flags int field in django admin?