Photo by josh Glauser on Unsplash

A common task when connecting to 3rd-party APIs is having to deal with API tokens. Usually, these tokens are most easily obtained by a single client authentication call to the API. In addition, they have timed lifespans, requiring you to regularly update them. Without any caching, you will most likely end up creating a new token for every call to the service, resulting in many redundant token requests and by extension, waste time on unnecessary API calls.

In this article, I will explain how to setup a robust system that can automatically cache and renew the API token on your…


Fallback Objects and Responses in Django — A few ways to implement them — Optimizing Django, Part 7

Django rocks!

When you serve objects in Django, sometimes you want to serve some sort of default data in the event that the object does not exist. This could come about for a variety of reasons: Maybe you just need to mock a response for your downstream clients while you work on the rest of the function, or you need to serve data for some querying user based on their profile, but need something generic to serve new users whilst you build that profile.

In this article, I will cover a couple of ways you can do this using Django, with differing…


Photo by Pablo Molina on Unsplash

If you’re using AWS (like I do on a regular basis), you’re probably familiar with AWS’s Kinesis queue service. According to Amazon’s website, “Amazon Kinesis Data Streams is a scalable and durable real-time data streaming service that can continuously capture gigabytes of data per second from hundreds of thousands of sources.” What’s more, Kinesis works seamlessly with Amazon’s other data-driven services, serverless applications like Lambda, data warehousing, and much more.

In this article, I will provide a short code sample of how you can push messages into a Kinesis data stream, and explain how to do it, although I’m not…


Note: In case it isn’t obvious already, big plot spoilers ahead, so you’ve been warned!

When it comes to Disney movies… actually, when it comes to most movies, I’m usually late to the party, preferring to wait for the reviews to come out, or until friends recommend them to me.

The Princess and The Frog, on the other hand, was different. I was introduced to the movie when I listened to its soundtrack on Spotify one day (Disney Hits, anyone?), …


Looking to work with Elasticsearch using Python? Look no further!

If you’re here, you might have heard of Elasticsearch, and maybe you want to know why you would prefer Elasticsearch to retrieve documents over, say, a plain SQL database.

Elasticsearch (or ES in short) is easily scaled up to handle massive amounts of documents, and offers a wide range of features that not only match SQL database functionality, but also surpasses in some instances. For example, ES does really well when it comes to text-based searching, being developed first as a search engine.

It’s open-source too, so you won’t have to worry about licenses and whatnot if you’re looking to…


As I sit at my (home) office desk, writing this article between bouts of work, I consider myself, strangely enough, grateful for this pandemic. Without this opportunity, I wouldn’t have come to know of the absolute freedom that working remotely comes with, and with it, the rush and crush of feelings that come with it.

Some time back, I wrote an article about how the pandemic has brought people to appreciate the upsides of remote work. But as many of us are beginning to realise, this great freedom is not entirely free of its own pitfalls and dangers. …


Coding quizzes are touted as a staple of developer interviews, ostensibly to filter out the silver from the dross, sift the wheat from the chaff. For interviewers, coding quizzes represent a cheap, readily available assessment of prospective job-seekers’ programming abilities. And not only are they great for assessing the coder’s style, leading coding quiz sites like Hackerrank even save snapshots of the state of the code as it is written, making it great for insights into how the coder thinks as he codes.

However, for non-developers or the otherwise uninformed, it is tempting to treat the coding quiz as an…


So far in this series, we’ve covered tips and tricks to reduce your SQL footprint in various parts of your Django server. For example, we’ve dealt with the n+1 problem, removed unnecessary SQL counts in our admin list page, and fixed up otherwise unusable Django admin pages that show data from related database tables. However, we haven’t covered caching, and to rectify this glaring omission, this article will explain how you can use it to save you a lot of response times where it is possible.

A quick word on caching before we dive into it…

With the use of a cache, it is possible to cut an immense amount…


This issue is quite notorious among Django Rest Framework users, so it’s always worth getting refreshed on it. If you use DRF or intend to use it, this article will explain how the n+1 problem can rear its ugly head in even the most innocuous of use cases, and how you can deal with it easily.

If you haven’t tried DRF, I wholeheartedly recommend you give it a try. It cuts out a lot of development work needed to get a REST framework up and running, while still being flexible enough to fulfil almost any programming needs you have

If…


In the previous entry of Optimizing Django, we covered one way to reduce our SQL footprint when using the Django admin at a minor cost of not rendering the correct object count in the table. However, even with this we can still run into situations where the Django admin becomes unusably slow. In this article, I will discuss a scenario which causes a Django admin changelist AND detail page to become terribly slow, what causes it, and how you can fix it to make it absolutely usable with minimal loss of functionality.

The scenario: You want to view a User’s Tickets in the user admin.

So picture this: You have a table full…

Mark Ang

Software Engineer and part-time blogger. Django is my bread-and-butter, but I also like trying new technologies. Follow me for Python and Django tech tips!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store