Fixing inefficient Django ORM calls in brownfield projects

def check_hounds():
queryset = HoundsModel.objects.all()
if len(queryset) > 2:
return "oh no. Run!"
def check_hounds():
if HoundsModel.objects.count() > 2:
return "oh no. Run!"
def write_condolence_letters(visited_coops):
queryset = ChickenModel.objects.all()
for chicken in queryset:
if chicken.coop.pk in visited_coops:
return f"dear {chicken.coop.owner_name}..."
else:
...
def write_condolence_letters(visited_coops):
queryset = ChickenModel.objects.all()
for chicken in queryset.iterator():
if chicken.coop_pk in visited_coops:
return f"dear {chicken.coop.owner_name}..."
else:
...
def write_condolence_letters(visited_coops):
queryset = ChickenModel.objects.all()
for chicken in queryset.select_related('coop'):
if chicken.coop.pk in visited_coops:
return f"dear {chicken.coop.owner_name}..."
else:
...

Does your codebase have old inefficient ORM lookups?

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store