I am writing a Django view that sometimes gets data from a database, and sometimes from an external API.
When it comes from the database, it is an instance of the Django model. Attributes must be accessible using dot notation.
Based on the API, the data is a dictionary and is accessible through musical notation.
In any case, some processing is done with the data.
I would like to avoid
if from_DB: item.image_url='http://example.com/{0}'.format(item.image_id) else: item['image_url']='http://example.com/{0}'.format(item['image_id'])
I am trying to find a more elegant, harsh way to do this.
Is there a way to get / set a key that works on dictionaries or objects?
source share