I have some rather long (~150 character) django queries. What's the preferred way to split them onto multiple lines?
For example (no, not my real code):
Edit: Changed the example because people were focused on repeating filter, not the length of the query:
person = models.UzbekistaniCitizen.objects.filter(occupation__income__taxable__gte=40000).exclude(face__eyes__color=blue).order_by('height').select_related('siblings', 'children')
Here are two ways I can think of:
Use backslashes as line breaks:
person = models.UzbekistaniCitizen.objects.\ filter(occupation__income__taxable__gte=40000).\ exclude(face__eyes__color=blue).\ order_by('height').\ select_related('siblings', 'children')
Re-apply the filter in new lines:
person = models.UzbekistaniCitizen.objects person = person.(occupation__income__taxable__gte=40000) person = person.exclude(face__eyes__color=blue) person = person.order_by('height') person = person.select_related('siblings', 'children')
You can use parentheses around the whole rhs to get implied line continuation:
person = (models.UzbekistaniCitizen .objects .filter(occupation__income__taxable__gte=40000) .exclude(face__eyes__color=blue) .order_by('height') .select_related('siblings', 'children'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With