Resolving Circular Imports and Performance Drops in Django

Day 33/90 How will circular imports affect performance? They don’t. They stop the application from starting entirely. Circular dependencies between serializers blocked the server today. Moving imports inside the __init__ method resolved the loop and allowed for the removal of 608 lines of redundant code. Resuming after a 7-day break following Day 32. Backend EdTech Project: • Query Count: Reduced database queries from 36 to 4 for student lists. I used prefetch_related with a Prefetch object to batch-load only the enrollment records relevant to the current course. • Refactoring: Consolidated five serializers and several views into a single ModelViewSet. This removed 608 lines of unnecessary code. • Search Fix: Resolved a 404 error in the search action. self.get_object() was incorrectly applying filters to the lookup; switching to a direct lookup fixed the routing. • Stability: Added checks in StudentSerializer to prevent KeyError crashes when specific fields are excluded. Code can pass tests and still be inefficient. I found an N+1 leak and a broken sort order today only because I looked at the raw SQL logs. Just because the server starts doesn't mean the logic is right. It’s a reminder to use automated test scripts and CaptureQueriesContext—I have to trust the logs. How do you catch performance drops in your code? Do you use query-count limits in your test suite, or do you have a different method for catching these issues before they reach a review? #python #django #backend #drf #90daysofcode #sql #refactoring #coding

  • diagram

For the technical implementation and documentation: • PR #107 for section 2 Courses: https://github.com/ft-mammoo/Gnowee-Training-PSC-Web/pull/107

Like
Reply

Don't stop, keep going, all eyes are on your progress 💪

See more comments

To view or add a comment, sign in

Explore content categories