Django Password Validators

By default Django comes with an advanced password validator, meaning user must enter password with a minimum length of 8 characters, some special sign, number. For production environment this is very important, however for development environment a simpler password would do and it wouldn’t take so much time. Today I have learned how to turn the advaced password protection off (remember to remove this line for production). Add the following line to your settings.py file: ...

August 31, 2023 · 1 min · 79 words

Asynchronous tasks with Celery & RabbitMQ

The Challenge I’m currently working on an application in Django which I hope to deploy in the upcoming weeks, it’s just that I fix one bug and another one appears, then tests pass, I add something new, suddenly nothing is working so I have to redesign the whole thing again. This has also prompted me to explore the topic of debugging a bit more recently. I guess that’s how we learn. ...

January 18, 2023 · 3 min · 525 words

Django & Docker

I have been reading about Docker for quite some time now. This term would pop up very often whenever I was checking something regarding Django. It wasn’t until recently that I actually made time to read up about it and understand what Docker is. Needless to say, I was missing out. I have now started using Docker with every project, and it makes the development process a lot more manageable. Obviously, I probably understand about 10% of all the features, but I am learning and already these 10% are of tremendous help. ...

December 28, 2022 · 2 min · 292 words

Using Redis for better performance

Sometimes your application runs multiple queries that can get very expensive in terms of performance. There are many tools and options when it comes to performance-optimization, and one of them is called caching. Caching refers to the process of storing data in a temporary location. When a request is made, system will first check this cached file to check if the data is available. It is very useful when the data is frequently accessed and/or the database is under high load. ...

December 27, 2022 · 2 min · 350 words

Django Best Practices - Meta class

In Django, properly defining database models is arguably the most important part of a new project. It is however very flexible in how we structure our models. Official Django coding style recommends following order: choices database fields custom manager attributes Meta def __str__() def save() def get_absolute_url() custom methods Today I want to look at some good practices I have learned so far regarding the Meta class. Explicitly name your model, not just your fields. For this, you would use verbose_name and verbose_name_plural (Django would just add an s to make it plural, citys, which is not correct) 1 2 3 class Meta: verbose_name = 'city' verbose_name_plural = 'cities' ordering is also very common - it defines the default order of a list of objects - however, ordering can be a performance hit, so as Django’s documentation says - “Don’t order results if you don’t care” If you do care, and want to use ordering, it is also a good idea to use indexing The Meta class is very powerful and there is of course a lot more, but these are just some options I have used recently. ...

December 22, 2022 · 1 min · 188 words

Setting up Index in your Models

When setting up models in Django, it is recommended to set an index for the specific fields of that Model. Index is a performance-enhancing feature, that allows a database to quickly locate and retrieve specific data from a table. It makes searching/sorting data in the database easier and more efficient. Here are some reasons why you might want to set an index on a field in a Django model: To improve the performance of queries that filter or sort data based on the indexed field. To enforce uniqueness constraints on the field. To improve the performance of foreign key relationships by creating a link between the primary key of one table and the indexed field of another table. Index class is located in the Meta inner class of your model. ...

December 21, 2022 · 2 min · 249 words

Complex database queries

Django comes with excellent ORM - Object Relational Mapper for querying the database. It’s very good for most cases, but sometimes you need to handle more complex queries. When you need to run complex queries, Django offers the Q Object. When you need to retrieve something from the database you would use .filter() method. 1 posts = Post.objects.filter(title__startswith="How to") This gives you all the posts whose title starts with “How to..”. What if you want to further filter the posts by another condition? That’s when Q Object comes in. ...

December 19, 2022 · 1 min · 195 words

Using Custom User Model

Django is currently my favorite framework when it comes to building web applications. I was first introduced to Django during my course CS50’s Web Programming with Python and JavaScript. It’s a fully featured framework, that comes with pretty much everything one would need for a modern web application. But what’s even more important is, the framework is very flexible and customizable. And just like for Python, there is a huge network of 3rd party packages and huge community around Django. ...

December 12, 2022 · 3 min · 456 words