502 Bad Gateway on Projects > Versions page
The Projects > Versions page for our project is throwing a 502 Bad Gateway error. It's happening at the following URL: 

https://readthedocs.org/projects/htcondor/versions

I thought this might be a transient error but it's been happening consistently for the last day or so. All the other pages I've tried seem to work fine.

It's worth noting a similar ticket  #5441  was filed a couple of days ago for a 502 error on the Import a Repository page. This seems to be working correctly now.

## Details

* Read the Docs project URL: https://htcondor.readthedocs.io
* Build URL (if applicable): 
* Read the Docs username (if applicable): htcondor

## Expected Result

A list of the different versions of our docs, dropdown menus to connect them to different branches, etc.

## Actual Result

Server Error

502 - Web server received an invalid response while acting as a gateway or proxy server.

There is a problem with the page you are looking for, and it cannot be displayed. When the Web server (while acting as a gateway or proxy) contacted the upstream content server, it received an invalid response from the content server.

diff --git a/readthedocs/core/permissions.py b/readthedocs/core/permissions.py
index 1c397eae1..a3bfa0423 100644
--- a/readthedocs/core/permissions.py
+++ b/readthedocs/core/permissions.py
@@ -9,6 +9,8 @@ class AdminPermissionBase:
 
     @classmethod
     def is_admin(cls, user, project):
+        # This explicitly uses "user in project.users.all" so that
+        # users on projects can be cached using prefetch_related or prefetch_related_objects
         return user in project.users.all() or user.is_superuser
 
     @classmethod
diff --git a/readthedocs/projects/views/public.py b/readthedocs/projects/views/public.py
index 2976cecb6..cc945b196 100644
--- a/readthedocs/projects/views/public.py
+++ b/readthedocs/projects/views/public.py
@@ -13,6 +13,7 @@ from django.contrib import messages
 from django.contrib.auth.models import User
 from django.core.cache import cache
 from django.core.files.storage import get_storage_class
+from django.db.models import prefetch_related_objects
 from django.http import HttpResponse, HttpResponseRedirect
 from django.shortcuts import get_object_or_404, render
 from django.urls import reverse
@@ -271,6 +272,9 @@ def project_versions(request, project_slug):
     if wiped and wiped_version.count():
         messages.success(request, 'Version wiped: ' + wiped)
 
+    # Optimize project permission checks
+    prefetch_related_objects([project], 'users')
+
     return render(
         request,
         'projects/project_version_list.html',
['readthedocs/core/permissions.py', 'readthedocs/projects/views/public.py']