Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Worst practices in Django you have ever seen [closed]

Tags:

What are the worst mistakes made using Django framework, that you have noticed? Have you seen some real misuses, that maybe should go as warnings to the Django docs?

like image 526
gruszczy Avatar asked Jan 13 '10 16:01

gruszczy


People also ask

Why is Django hard?

Yes, learning django is hard, but if your project grows up don't learn django is harder. Really. I'm also learning Django right now and I think, the complexity comes from the various components you have to learn (front-end, back end), all the various languages you need (Django + Javascript + CSS + HTML).

Is Django low code?

One reason I think Django would be a good case is because it already has many things setup for at least a low-code tool. It has an admin panel out of the box and ready to use user models. IMO it already beats the some no-code tools in the “user” aspect and I find it much easier to create models in Django.

How does Django handle large data?

Use bulk query. Use bulk queries to efficiently query large data sets and reduce the number of database requests. Django ORM can perform several inserts or update operations in a single SQL query. If you're planning on inserting more than 5000 objects, specify batch_size.


2 Answers

Too much logic in views.

I used to write views that would struggle to fit in 40 lines. Now I consider more than 2-3 indentation levels, 10 or so LOC or a handful of inline comments in a view to be code smells.

The temptation is to write minimal models, figure out your url routing, then do everything else in the view. In reality, you should be using model methods, managers, template tags, context processors, class-based views with abstract base views... anything to keep the view code simple and readable. Logic around saving forms should go in Form.save(). Logic repeated at the start or end of multiple views should go in decorators. Reused display logic should go in included templates, template tags, and filters.

Long views are hard to read, understand, and debug. Learn to use the other tools in you toolkit any you'll save yourself and your team a lot of pain.

like image 90
ozan Avatar answered Oct 11 '22 21:10

ozan


Not splitting stuff up into multiple applications. It's not so much about reusability as it is about having a dozen models, and over 100 views in one app, it's damned unreadable. Plus I like to be able to scan my urls.py file easily to see where a URL points, when I have 100 URLs that gets harder.

like image 39
Alex Gaynor Avatar answered Oct 11 '22 21:10

Alex Gaynor