8👍
How about
for obj in (queryset or []):
# Do your stuff
It is the same as J.F Sebastians suggestion, only not implemented as a list comprehension.
- [Django]-Django not working with supervisor, getting [Errno 88] Socket operation on non-socket
- [Django]-Django replaces non-ascii characters with \ufffd
2👍
For what it’s worth, Django managers have a “none” queryset that you can use to avoid gratuitous None-checking. Using it to ensure you don’t have a null queryset may simplify your code.
if queryset is None:
queryset = MyModel.objects.none()
References:
1👍
you can use list comprehensions, but other than that I don’t see what you can improve
result = []
if queryset:
result = [(getattr(obj, field.attname), obj.pk) for obj in queryset]
- [Django]-Django-nginx-gunicorn – not working
- [Django]-Django admin ignores has_delete_permission
- [Django]-Django over https form redirection issues
- [Django]-Login and registration form on one page with Django
- [Django]-NoReverseMatch in django production server
Source:stackexchange.com