DEV Community

Cover image for Do's and Don'ts in Django
Papan Sarkar
Papan Sarkar

Posted on

Do's and Don'ts in Django


Use meaningful variables: We should use clear, concise, and self-explanatory variables, functions, and classes name.

DRY: Don’t Repeat Yourself. We should reuse code as much as possible. Once you are done with the code, take 10 minutes to think what the possibilities are to improve it further and make it optimal.

Use Django features: On daily basis, we receive unique requirements which is consist of unique business logic for those cases we often write flexible code to solve a problem, which is fine but again it might not be the optimal solution. So again, take 10 minutes extra to research how could you have done this differently keeping a standard approach and solution.

Quick related note: It is very normal to think of a custom solution first, however it helps or comes to your head. No one solves a problem in the first go with the best possible solution and standards. It is always the next set of iteration which makes the difference.

Do a security check: Always do a security check once you are done with a task. Think of edge cases which can eventually create a problem. If needed get on a call with someone and share your thoughts and understanding, you will get more clarity with another set of eyes.

Follow PEP8: Since so many people are working on the same code, and we are trying to make it more readable and understandable. It is especially important that we make use PEP 8 style guide for Python code.


Do not write Complex Queries on serializers: Avoid writing complex queries on serializers or any serializer methods. Prefetch data in views instead.

Don’t write generic views all the time: Generic views are super useful and powerful but do not use it when it is not required. If the requirement is simple and straightforward write Functional views instead.

**Don’t keep unlimited access to endpoints (Rate Limiting): **Implement rate limiting to protect APIs.

Don’t write nested serializers: Nested serializers can produce inefficient queries and effect performance. Create different endpoints for these cases or change the data structure.

Don’t write business logic inside views or serializers: It is one of most important things to consider when building a huge project consists of lots of business logic. Business logic should be written in service layer, model property methods etc.

Top comments (0)