DEV Community

Joseph Mania
Joseph Mania

Posted on • Originally published at

Mistakes To Avoid As A Django Developer

Django is a python web framework that deals with the server side. You can use it to develop a complete web portal if you have masters HTML,CSS ad little JavaScript. I cant say am a pro, but one year of experience has made me learn a lot of tricks. But sometimes I have reviewed my friend’s code. Here are the common mistakes I narrowed down:

Building things from scratch

Remember, one of the most important aspects of Django is to speed up the rate of development process. There are a lot of third party libraries like form. Be simple and use the libraries, if necessary. Such includes employing Anymail while sending emails, use django-allauth for registration portal. Also, starting your project with cookiecutter might offer you a lot of privileges such as built in user models.

Writing skinny models

Don’t force data into one model. Let your models be fat and self-explanatory. It will make it easier to query in your view. In short, Django architecture is also called Model-Template_view. Write as many model lines as possible to make it easier to query your database. Remember that the model acts as the heart of the database. If it’s fine, it’s going to be easier to display the data in templates.

Having too many queries in View

This will automatically mean that the model was poorly developed. You must learn the relationship between different models. There is no shortcut. Use of foreignkeys, oneToOne, ManyToMany relationship and others. Optimize your ORM queries as you fix any broken queries. Make sure the while adding a cache, it’s in the right position.
I would suggest you use django-debug-toolbar to investigate the performance of SQL querries, cache, templates and requests.

Avoiding Indexes on Models

This is mistake done by almost every Django developer. Adding an index will accelerate the rate at which data is retrieved. Just go through your querryset to see where the index is needed. If you use too much index they will mess up everything. More indexing will slow down the rate of data access.

Having Inconsistency Form Validation

Of course there are many forms in Django with various valdation format. While working on the form in the model, make sure you use the best constraints possible. Take note of the null and blank values, which at some point might confuse. Any inconsistency in the form will contradict the user. In some points the form might not be submitted.

Top comments (0)