Making Django website faster than before just using simple tricks
Today we are going to learn about some really simple tricks that will make your Django website faster than before
So basically I am a Django developer and the thing I don’t like while making a website in Django is that sometimes database querying is very slow which eventually leads to a slow site and we know the pain that happens when our website does not load fast enough.
So what we can do to make our website load faster?
Don’t worry, I am here to help you, today I am going to tell you 4 easy ways to make your Django website faster before.
So let get started.
1.Using for loop to add multiple data is kinda boring:
So when I was a newbie in Django, o to add multiple data points in my model I use for loop.
But you know what, It is a very slow process which leads to making your website speed slow.
So what is the best way to add multiple data points?
Simply use bulk_create() it is a database query API to create multiple data points at once.
The way to do that:
These code snippets are examples of a project I made.
In this code, I am making a list of Question models and then using bulk_create() and passing that list as an argument to create data points.
You can do that like this too:
This is the faster way to achieve the same type of things and believe me, I really follow this trick and got a lot of benefit from it.
Now let see what is different from normal looping?
In normal looping, you will create one data one by one which causes more load on the backend which eventually makes it slow but bulk_create() create all instances at once.
So let jump into our next tip.
2.Why you are updating data points one by one?:
So same as before, there is one functionality in Django that is updating any data instance.
You can update by using a loop or by just using bulk_update().
Updating any data point using a loop is very slow and takes a lot of time.
So simply use bulk_update().
So you can use bulk_update() like this:
This is how you can update multiple data points at once, It will reduce server load too.
Let jump to the next tip.
3.Using serializer for uploading images:
So uploading images is very important in Django but as you know images are very heavy so there is a very problem uploading them, especially if you are using Django rest API and you have a frontend like reactJS.
So what you can do about this?
You can use an APIView and parser classes for this, it is the fastest and easiest way to do this.
Let me show you how you can do this.
This is the best way to upload an image through API.
A post-function inside APIView is needed and you are all good to work.
I know sometimes you come across ViewSet and Serializers, for uploading images, that is a great method too, but sometimes it gets irritating errors, so I thought simply using APIView is better and it will help you to avoid irritating errors.
Serializers are always great to use with APIs cause it is very fast.
4.Best way to create a single instance:
So whenever you create any instance you have two methods to do that, let me show you both.
One method is simply using .create() like this:
Obviously, this is a great method but I find a thing while using it.
When you have more than 5 values to put then it is a super slow method. You will find it take a lot of time to just creating one instance.
Sometimes, it even throws some irritating errors.
So what is the best way to create a single instance?
So let talk about another method.
The other method looks very little messed up but it is great for more than 5 values.
The method is:
This is the best method for more than 5 values
Believe me, it is the greatest method you ever saw.
It looks slow but it is great for a big project, more lines of code do not mean more complexity or slow processing.
These four tips are my own experimental results, and I made my online assignment management system(just for practice) faster.
And these tips are my 2 months of regular practice and works results so don’t ignore this, these are masterpieces.
Thank you and with this, I will end my article.
.Follow me on Instagram:https://www.instagram.com/aviar.org_/
You can buy my code: Here is the link