234 points by johndoe 6 months ago flag hide 18 comments
john_doe 6 months ago next
Great guide! I've been using Django for years and never realized how much optimizing I could do. Thank you for sharing!
original_poster 6 months ago next
@john_doe Thanks for the kind words! I'm glad you found it helpful.
john_doe 6 months ago prev next
@original_poster Can you give some more info on how to optimize views specifically?
original_poster 6 months ago next
@john_doe Sure! My go-to approach is to use the `select_related` and `prefetch_related` functions to pre-fetch related objects and reduce database queries. Also, using caching at the view level can make a huge difference.
random_user 6 months ago prev next
I'm thinking of switching over to Django soon. Do you have any tips for getting started?
original_poster 6 months ago next
@random_user Definitely check out the Django documentation and do some tutorials to get started. And don't forget to keep this guide in mind for optimizing later on.
random_user 6 months ago next
@original_poster Thanks, I'll definitely check out the resources you mentioned. Quick question, do you have experience with optimizing Postgres?
original_poster 6 months ago next
@random_user Yes, I have some experience. The guide touches on some Postgres optimization techniques, but some things to keep in mind are indexing, vacuuming, and using the query planner to optimize.
helpful_hints 6 months ago prev next
One thing I would add is to make sure to use caching whenever possible to speed up page load times.
another_tip 6 months ago prev next
Don't forget to take a look at the Django Debug Toolbar for some good insights on optimizing your app.
question_about_profiling 6 months ago prev next
I heard that a good way to optimize is to profile. What tools do you recommend using for Django profiling?
original_poster 6 months ago next
@question_about_profiling I recommend the Django Debug Toolbar for a user interface for profiling, and the `django-silk` library for more detailed profiling.
another_reccomendation 6 months ago prev next
Another option is the `pytest-django` library which includes built-in support for database query introspection and performance profiling in tests.
opinion_on_orm 6 months ago prev next
I prefer using an ORM over raw SQL, but some say it's slower. What's your opinion on this?
original_poster 6 months ago next
@opinion_on_orm ORMs can definitely be slower, but the convenience they offer in terms of development speed and maintainability can be worth the trade-off. With good optimization practices, an ORM can perform well enough for most applications.
previous_commenter 6 months ago prev next
I agree with @original_poster about ORMs having a trade-off. But after my experience working with hundreads of queries with raw SQL, I'll take the ORM route anyday.
memory_leak_issue 6 months ago prev next
I'm having trouble with a memory leak in my Django app. Any advice on how to go about debugging this?
experienced_user 6 months ago next
@memory_leak_issue I would start by using a memory profiler such as `memory-profiler` or `psutil` to see what's causing the leak. And also, use python's built-in `gc` library to check for and clear garbage collections.