Python django.db.models.Prefetch() Examples

The following are 30 code examples for showing how to use django.db.models.Prefetch(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module django.db.models , or try the search function .

Example 1
Project: seqr   Author: macarthur-lab   File: orm_to_json_utils.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def get_json_for_variant_tags(tags, add_variant_guids=True):
    """Returns a JSON representation of the given variant tags.

    Args:
        tag (object): Django models for the VariantTag.
    Returns:
        dict: json objects
    """
    def _process_result(tag_json, tag):
        if add_variant_guids:
            tag_json['variantGuids'] = [variant.guid for variant in tag.saved_variants.all()]

    if add_variant_guids:
        prefetch_related_objects(tags, Prefetch('saved_variants', queryset=SavedVariant.objects.only('guid')))

    nested_fields = [{'fields': ('variant_tag_type', field), 'key': field} for field in ['name', 'category', 'color']]
    return _get_json_for_models(tags, nested_fields=nested_fields, guid_key='tagGuid', process_result=_process_result) 
Example 2
Project: seqr   Author: macarthur-lab   File: orm_to_json_utils.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def get_json_for_variant_functional_data_tags(tags, add_variant_guids=True):
    """Returns a JSON representation of the given variant tags.

    Args:
        tags (object): Django models for the VariantFunctionalData.
    Returns:
        dict: json objects
    """

    def _process_result(tag_json, tag):
        display_data = json.loads(tag.get_functional_data_tag_display())
        tag_json.update({
            'name': tag_json.pop('functionalDataTag'),
            'metadataTitle': display_data.get('metadata_title'),
            'color': display_data['color'],
        })
        if add_variant_guids:
            tag_json['variantGuids'] = [variant.guid for variant in tag.saved_variants.all()]

    if add_variant_guids:
        prefetch_related_objects(tags, Prefetch('saved_variants', queryset=SavedVariant.objects.only('guid')))

    return _get_json_for_models(tags, guid_key='tagGuid', process_result=_process_result) 
Example 3
Project: seqr   Author: macarthur-lab   File: orm_to_json_utils.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def get_json_for_variant_notes(notes, add_variant_guids=True):
    """Returns a JSON representation of the given variant notes.

    Args:
        notes (object): Django model for the VariantNote.
    Returns:
        dict: json objects
    """
    def _process_result(note_json, note):
        if add_variant_guids:
            note_json['variantGuids'] = [variant.guid for variant in note.saved_variants.all()]

    if add_variant_guids:
        prefetch_related_objects(notes, Prefetch('saved_variants', queryset=SavedVariant.objects.only('guid')))

    return _get_json_for_models(notes, guid_key='noteGuid', process_result=_process_result) 
Example 4
Project: PrivacyScore   Author: PrivacyScore   File: views.py    License: GNU General Public License v3.0 6 votes vote down vote up
def scan_scan_list(request: HttpRequest, scan_list_id: int) -> HttpResponse:
    """Schedule the scan of a scan list."""
    scan_list = get_object_or_404(
        ScanList.objects.prefetch_related(Prefetch(
            'sites',
            queryset=Site.objects.select_related('last_scan') \
                .annotate_most_recent_scan_start() \
                .annotate_most_recent_scan_end_or_null())
        ), pk=scan_list_id)
    was_any_site_scannable = scan_list.scan()
    if was_any_site_scannable:
        num_scanning_sites = Scan.objects.filter(end__isnull=True).count()
        messages.success(request,
            _("Scans for this list have been scheduled. "+ \
              "The total number of sites in the scanning queue "+ \
              "is %i (including yours)." % num_scanning_sites))
    else:
        messages.warning(request,
            _('All sites have been scanned recently. Please wait 30 minutes and try again.'))

    return redirect(reverse('frontend:view_scan_list', args=(scan_list_id,))) 
Example 5
Project: pasportaservo   Author: tejoesperanto   File: profiles.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        display_places = self.object.owned_places.filter(deleted=False)
        if self.public_view:
            display_places = display_places.filter(visibility__visible_online_public=True)
        else:
            family_members = Prefetch(
                'family_members',
                Profile.all_objects.order_by('birth_date').select_related('user'))
            display_places = display_places.prefetch_related(family_members)
        if self.request.user.has_perm(PERM_SUPERVISOR):
            display_places = (display_places
                              .select_related('checked_by', 'checked_by__profile')
                              .defer('checked_by__profile__description'))
        context['places'] = display_places.select_related('visibility')

        display_phones = self.object.phones.filter(deleted=False)
        context['phones'] = display_phones
        context['phones_public'] = display_phones.filter(visibility__visible_online_public=True).select_related(None)

        return context 
Example 6
Project: civet   Author: idaholab   File: EventsStatus.py    License: Apache License 2.0 6 votes vote down vote up
def get_default_events_query(event_q=None):
    """
    Default events query that preloads all that will be needed in events_info()
    Input:
      event_q: An existing models.Event query
    Return:
      a query on models.Event
    """
    if event_q == None:
        event_q = models.Event.objects

    jobs_q = models.Job.objects.select_related('config', 'recipe'
            ).prefetch_related('recipe__build_configs','recipe__depends_on',)
    return event_q.order_by('-created').select_related(
        'base__branch__repository__user__server',
        'head__branch__repository__user__server',
        'pull_request',
        ).prefetch_related(Prefetch('jobs', queryset=jobs_q)) 
Example 7
Project: civet   Author: idaholab   File: RepositoryStatus.py    License: Apache License 2.0 6 votes vote down vote up
def get_user_repos_with_open_prs_status(username, last_modified=None):
    """
    Get a list of open PRs for a user, grouped by repository and sorted by repository name
    Input:
      user[models.GitUser]: The user to get the status for
    Return:
      list of dicts containing repository information
    """
    pr_q = models.PullRequest.objects.filter(closed=False, username=username).order_by("number")

    if last_modified:
        pr_q = pr_q.filter(last_modified__gte=last_modified)
    repo_q = repos = models.Repository.objects.filter(pull_requests__username=username, pull_requests__closed=False).distinct()
    repos = (repo_q
                .order_by("name")
                .prefetch_related(Prefetch('pull_requests', queryset=pr_q, to_attr='open_prs'))
                .select_related("user__server"))

    return get_repos_data(repos) 
Example 8
Project: osler   Author: SaturdayNeighborhoodHealthClinic   File: views.py    License: GNU General Public License v3.0 6 votes vote down vote up
def all_patients(request):
    """
    Query is written to minimize hits to the database; number of db hits can be
        see on the django debug toolbar.
    """
    patient_list = mymodels.Patient.objects.all() \
        .order_by('last_name') \
        .select_related('gender') \
        .prefetch_related('case_managers') \
        .prefetch_related(Prefetch('workup_set', queryset=workupmodels.Workup.objects.order_by('clinic_day__clinic_date'))) \
        .prefetch_related('actionitem_set')

    # Don't know how to prefetch history https://stackoverflow.com/questions/45713517/use-prefetch-related-in-django-simple-history
    # Source code is https://github.com/treyhunner/django-simple-history/blob/master/simple_history/models.py if we want to try to figure out

    return render(request,
                  'pttrack/all_patients.html',
                  {'object_list': patient_list}) 
Example 9
Project: dynamic-rest   Author: AltSchool   File: prefetch.py    License: MIT License 6 votes vote down vote up
def prefetch_related(self, *args):
        try:
            for arg in args:
                if isinstance(arg, str):
                    arg = FastPrefetch.make_from_field(
                        model=self.model,
                        field_name=arg
                    )
                elif isinstance(arg, Prefetch):
                    arg = FastPrefetch.make_from_prefetch(arg, self.model)
                if not isinstance(arg, FastPrefetch):
                    raise Exception("Must be FastPrefetch object")

                if arg.field in self.prefetches:
                    raise Exception(
                        "Prefetch for field '%s' already exists."
                    )
                self.prefetches[arg.field] = arg
        except Exception as e:  # noqa
            traceback.print_exc()

        return self 
Example 10
Project: dynamic-rest   Author: AltSchool   File: prefetch.py    License: MIT License 6 votes vote down vote up
def _get_django_queryset(self):
        """Return Django QuerySet with prefetches properly configured."""

        prefetches = []
        for field, fprefetch in self.prefetches.items():
            has_query = hasattr(fprefetch, 'query')
            qs = fprefetch.query.queryset if has_query else None
            prefetches.append(
                Prefetch(field, queryset=qs)
            )

        queryset = self.queryset
        if prefetches:
            queryset = queryset.prefetch_related(*prefetches)

        return queryset 
Example 11
Project: lexpredict-contraxsuite   Author: LexPredict   File: views.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def get_context_data(self, **kwargs):
        document = self.object
        paragraph_list = document.textunit_set \
            .filter(unit_type='paragraph') \
            .order_by('id') \
            .prefetch_related(
                models.Prefetch(
                    'termusage_set',
                    queryset=TermUsage.objects.order_by('term__term').select_related('term'),
                    to_attr='ltu'))
        ctx = {'document': document,
               'party_list': list(PartyUsage.objects.filter(
                   text_unit__document=document).values_list('party__name', flat=True)),
               'highlight': self.request.GET.get('highlight', ''),
               'paragraph_list': paragraph_list}
        return ctx 
Example 12
Project: SchoolIdolAPI   Author: MagiCircles   File: views.py    License: Apache License 2.0 6 votes vote down vote up
def _ajaxaccounttab_teams(tab, request, account, more):
    context = {}
    if account.owner == request.user:
        context['is_me'] = True
    teams = models.Team.objects.filter(owner_account=account).prefetch_related(Prefetch('members', queryset=models.Member.objects.select_related('ownedcard', 'ownedcard__card').order_by('position'), to_attr='all_members'))
    range_aligners = [0,1,2,3,4,3,2,1,0]
    for team in teams:
        team.owner_account = account
        members = [{'position': i, 'virtual': True, 'range_align': range(range_aligners[i])} for i in range(9)]
        for member in team.all_members:
            member.range_align = members[member.position]['range_align']
            members[member.position] = member
        team.all_members = members
    account.all_teams = teams
    context['account'] = account
    return context 
Example 13
Project: Spirit   Author: nitely   File: forms.py    License: MIT License 6 votes vote down vote up
def _populate_choices(self):
        # This is *hackish* but simpler than subclassing ModelChoiceIterator
        choices = [("", self.empty_label)]
        kwargs = {self.parent_field: None}
        queryset = (
            self.queryset
            .filter(**kwargs)
            .prefetch_related(Prefetch(self.related_name, queryset=self.queryset)))

        for parent in queryset:
            choices.append((self.prepare_value(parent), self.label_from_instance(parent)))
            choices.extend(
                (self.prepare_value(children), self.label_from_instance(children))
                for children in getattr(parent, self.related_name).all())

        self.choices = choices 
Example 14
Project: yournextrepresentative   Author: mysociety   File: popolo_extra.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def joins_for_csv_output(self):
        return self.select_related('base') \
            .prefetch_related(
                models.Prefetch(
                    'base__memberships',
                    Membership.objects.select_related(
                        'extra',
                        'extra__election',
                        'on_behalf_of__extra',
                        'post__area',
                        'post__extra',
                    ).prefetch_related(
                        'on_behalf_of__identifiers',
                        'post__area__other_identifiers',
                    )
                ),
                'base__contact_details',
                'base__identifiers',
                'base__links',
                'images__extra__uploading_user',
            ) 
Example 15
Project: instiapp-api   Author: wncc   File: serializers.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def setup_eager_loading(queryset, request, extra_prefetch=None):
        """Perform necessary eager loading of data."""

        # Get the fields to be prefetched
        fields = ['bodies', 'venues', 'user_tags', 'offered_achievements']

        # Add prefetch for user_ues
        if request.user.is_authenticated and hasattr(request.user, 'profile'):
            user_query = UserEventStatus.objects.filter(user_id=request.user.profile.id)
            fields.append(Prefetch('ues', queryset=user_query, to_attr='uues'))

        # Add extra prefetch fields
        if extra_prefetch:
            fields += extra_prefetch

        queryset = queryset.prefetch_related(*fields)

        # Prefetch counts
        interested_count = Count('followers', distinct=True, filter=Q(ues__status=1))
        going_count = Count('followers', distinct=True, filter=Q(ues__status=2))
        queryset = queryset.annotate(interested_count=interested_count).annotate(going_count=going_count)

        return queryset 
Example 16
Project: graphene-django-optimizer   Author: tfoxy   File: query.py    License: MIT License 6 votes vote down vote up
def select_related(self, name, store):
        if store.select_list:
            for select in store.select_list:
                self.select_list.append(name + LOOKUP_SEP + select)
        else:
            self.select_list.append(name)
        for prefetch in store.prefetch_list:
            if isinstance(prefetch, Prefetch):
                prefetch.add_prefix(name)
            else:
                prefetch = name + LOOKUP_SEP + prefetch
            self.prefetch_list.append(prefetch)
        if self.only_list is not None:
            if store.only_list is None:
                self.abort_only_optimization()
            else:
                for only in store.only_list:
                    self.only_list.append(name + LOOKUP_SEP + only) 
Example 17
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def test_m2m(self):
        # Control lookups.
        with self.assertNumQueries(2):
            lst1 = self.traverse_qs(
                Person.objects.prefetch_related('houses'),
                [['houses']]
            )

        # Test lookups.
        with self.assertNumQueries(2):
            lst2 = self.traverse_qs(
                Person.objects.prefetch_related(Prefetch('houses')),
                [['houses']]
            )
        self.assertEqual(lst1, lst2)
        with self.assertNumQueries(2):
            lst2 = self.traverse_qs(
                Person.objects.prefetch_related(Prefetch('houses', to_attr='houses_lst')),
                [['houses_lst']]
            )
        self.assertEqual(lst1, lst2) 
Example 18
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def test_reverse_m2m(self):
        # Control lookups.
        with self.assertNumQueries(2):
            lst1 = self.traverse_qs(
                House.objects.prefetch_related('occupants'),
                [['occupants']]
            )

        # Test lookups.
        with self.assertNumQueries(2):
            lst2 = self.traverse_qs(
                House.objects.prefetch_related(Prefetch('occupants')),
                [['occupants']]
            )
        self.assertEqual(lst1, lst2)
        with self.assertNumQueries(2):
            lst2 = self.traverse_qs(
                House.objects.prefetch_related(Prefetch('occupants', to_attr='occupants_lst')),
                [['occupants_lst']]
            )
        self.assertEqual(lst1, lst2) 
Example 19
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def test_m2m_through_fk(self):
        # Control lookups.
        with self.assertNumQueries(3):
            lst1 = self.traverse_qs(
                Room.objects.prefetch_related('house__occupants'),
                [['house', 'occupants']]
            )

        # Test lookups.
        with self.assertNumQueries(3):
            lst2 = self.traverse_qs(
                Room.objects.prefetch_related(Prefetch('house__occupants')),
                [['house', 'occupants']]
            )
        self.assertEqual(lst1, lst2)
        with self.assertNumQueries(3):
            lst2 = self.traverse_qs(
                Room.objects.prefetch_related(Prefetch('house__occupants', to_attr='occupants_lst')),
                [['house', 'occupants_lst']]
            )
        self.assertEqual(lst1, lst2) 
Example 20
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def test_m2m_through_gfk(self):
        TaggedItem.objects.create(tag="houses", content_object=self.house1)
        TaggedItem.objects.create(tag="houses", content_object=self.house2)

        # Control lookups.
        with self.assertNumQueries(3):
            lst1 = self.traverse_qs(
                TaggedItem.objects.filter(tag='houses').prefetch_related('content_object__rooms'),
                [['content_object', 'rooms']]
            )

        # Test lookups.
        with self.assertNumQueries(3):
            lst2 = self.traverse_qs(
                TaggedItem.objects.prefetch_related(
                    Prefetch('content_object'),
                    Prefetch('content_object__rooms', to_attr='rooms_lst')
                ),
                [['content_object', 'rooms_lst']]
            )
        self.assertEqual(lst1, lst2) 
Example 21
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def test_traverse_single_item_property(self):
        # Control lookups.
        with self.assertNumQueries(5):
            lst1 = self.traverse_qs(
                Person.objects.prefetch_related(
                    'houses__rooms',
                    'primary_house__occupants__houses',
                ),
                [['primary_house', 'occupants', 'houses']]
            )

        # Test lookups.
        with self.assertNumQueries(5):
            lst2 = self.traverse_qs(
                Person.objects.prefetch_related(
                    'houses__rooms',
                    Prefetch('primary_house__occupants', to_attr='occupants_lst'),
                    'primary_house__occupants_lst__houses',
                ),
                [['primary_house', 'occupants_lst', 'houses']]
            )
        self.assertEqual(lst1, lst2) 
Example 22
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def test_traverse_multiple_items_property(self):
        # Control lookups.
        with self.assertNumQueries(4):
            lst1 = self.traverse_qs(
                Person.objects.prefetch_related(
                    'houses',
                    'all_houses__occupants__houses',
                ),
                [['all_houses', 'occupants', 'houses']]
            )

        # Test lookups.
        with self.assertNumQueries(4):
            lst2 = self.traverse_qs(
                Person.objects.prefetch_related(
                    'houses',
                    Prefetch('all_houses__occupants', to_attr='occupants_lst'),
                    'all_houses__occupants_lst__houses',
                ),
                [['all_houses', 'occupants_lst', 'houses']]
            )
        self.assertEqual(lst1, lst2) 
Example 23
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def test_custom_queryset(self):
        bookmark = Bookmark.objects.create(url='http://www.djangoproject.com/')
        django_tag = TaggedItem.objects.create(content_object=bookmark, tag='django')
        TaggedItem.objects.create(content_object=bookmark, tag='python')

        with self.assertNumQueries(2):
            bookmark = Bookmark.objects.prefetch_related(
                Prefetch('tags', TaggedItem.objects.filter(tag='django')),
            ).get()

        with self.assertNumQueries(0):
            self.assertEqual(list(bookmark.tags.all()), [django_tag])

        # The custom queryset filters should be applied to the queryset
        # instance returned by the manager.
        self.assertEqual(list(bookmark.tags.all()), list(bookmark.tags.all().all())) 
Example 24
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def test_retrieves_results_from_prefetched_objects_cache(self):
        """
        When intermediary results are prefetched without a destination
        attribute, they are saved in the RelatedManager's cache
        (_prefetched_objects_cache). prefetch_related() uses this cache
        (#27554).
        """
        authors = AuthorWithAge.objects.prefetch_related(
            Prefetch(
                'author',
                queryset=Author.objects.prefetch_related(
                    # Results are saved in the RelatedManager's cache
                    # (_prefetched_objects_cache) and do not replace the
                    # RelatedManager on Author instances (favorite_authors)
                    Prefetch('favorite_authors__first_book'),
                ),
            ),
        )
        with self.assertNumQueries(4):
            # AuthorWithAge -> Author -> FavoriteAuthors, Book
            self.assertQuerysetEqual(authors, ['<AuthorWithAge: Rousseau>', '<AuthorWithAge: Voltaire>']) 
Example 25
Project: PrivacyScore   Author: PrivacyScore   File: models.py    License: GNU General Public License v3.0 5 votes vote down vote up
def prefetch_columns(self) -> 'ScanListQuerySet':
        return self.prefetch_related(
            Prefetch(
                'columns',
                queryset=ListColumn.objects.order_by('sort_key'),
                to_attr='sorted_columns')) 
Example 26
Project: PrivacyScore   Author: PrivacyScore   File: models.py    License: GNU General Public License v3.0 5 votes vote down vote up
def prefetch_tags(self) -> 'ScanListQuerySet':
        return self.prefetch_related(
            Prefetch(
                'tags',
                queryset=ListTag.objects.order_by('name'),
                to_attr='ordered_tags')) 
Example 27
Project: PrivacyScore   Author: PrivacyScore   File: models.py    License: GNU General Public License v3.0 5 votes vote down vote up
def prefetch_column_values(self, scan_list: ScanList) -> 'SiteQuerySet':
        return self.prefetch_related(Prefetch(
                'column_values',
                queryset=ListColumnValue.objects.filter(
                    column__scan_list=scan_list).order_by(
                    'column__sort_key'),
                to_attr='ordered_column_values')
        ) 
Example 28
Project: casepro   Author: rapidpro   File: models.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def search(cls, org, user, search):
        """
        Search for cases
        """
        folder = search.get("folder")
        assignee_id = search.get("assignee")
        after = search.get("after")
        before = search.get("before")

        if folder == CaseFolder.open:
            queryset = Case.get_open(org, user)
        elif folder == CaseFolder.closed:
            queryset = Case.get_closed(org, user)
        else:  # pragma: no cover
            raise ValueError("Invalid folder for cases")

        if assignee_id:
            queryset = queryset.filter(assignee__pk=assignee_id)

        if after:
            queryset = queryset.filter(opened_on__gte=after)
        if before:
            queryset = queryset.filter(opened_on__lte=before)

        queryset = queryset.select_related("contact", "assignee", "user_assignee")

        queryset = queryset.prefetch_related(Prefetch("labels", Label.objects.filter(is_active=True)))

        return queryset.order_by("-opened_on") 
Example 29
Project: civet   Author: idaholab   File: views.py    License: Apache License 2.0 5 votes vote down vote up
def view_job(request, job_id):
    """
    View the details of a job, along
    with any results.
    """
    recipe_q = models.Recipe.objects.prefetch_related("depends_on", "auto_authorized", "viewable_by_teams")
    q = (models.Job.objects
            .select_related('recipe__repository__user__server',
                'recipe__build_user__server',
                'event__pull_request',
                'event__base__branch__repository__user__server',
                'event__head__branch__repository__user__server',
                'config',
                'client',)
            .prefetch_related(Prefetch("recipe", queryset=recipe_q),
                'step_results',
                'changelog'))
    job = get_object_or_404(q, pk=job_id)
    perms = Permissions.job_permissions(request.session, job)
    clients = None
    if perms['can_see_client']:
        clients = sorted_clients(models.Client.objects.exclude(status=models.Client.DOWN))
    perms['job'] = job
    perms['clients'] = clients
    perms['update_interval'] = settings.JOB_PAGE_UPDATE_INTERVAL
    return render(request, 'ci/job.html', perms) 
Example 30
Project: civet   Author: idaholab   File: RepositoryStatus.py    License: Apache License 2.0 5 votes vote down vote up
def get_repos_status(repo_q, last_modified=None):
    """
    Get a list of open PRs, grouped by repository and sorted by repository name
    Input:
      repo_q: A query on models.Repository
      last_modified: DateTime: if records with last_modified are before this they are ignored
    Return:
      list of dicts containing repository information
    """
    branch_q = models.Branch.objects.exclude(status=models.JobStatus.NOT_STARTED)
    badge_q = models.RepositoryBadge.objects.exclude(status=models.JobStatus.NOT_STARTED)
    if last_modified is not None:
        branch_q = branch_q.filter(last_modified__gte=last_modified)
        badge_q = badge_q.filter(last_modified__gte=last_modified)
        repo_q.filter(last_modified__gte=last_modified)
    branch_q = branch_q.order_by('name')
    badge_q = badge_q.order_by('name')

    pr_q = models.PullRequest.objects.filter(closed=False)
    if last_modified:
        pr_q = pr_q.filter(last_modified__gte=last_modified)
    pr_q = pr_q.order_by('number')

    repos = (repo_q.order_by('name')
                .prefetch_related(Prefetch('branches', queryset=branch_q, to_attr='active_branches'))
                .prefetch_related(Prefetch('pull_requests', queryset=pr_q, to_attr='open_prs'))
                .prefetch_related(Prefetch('badges', queryset=badge_q, to_attr="active_badges"))
                .select_related("user__server"))

    return get_repos_data(repos)