Python django.db.models.Prefetch() Examples

The following are 30 code examples of django.db.models.Prefetch(). 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 also want to check out all available functions/classes of the module django.db.models , or try the search function .
Example #1
Source File: orm_to_json_utils.py    From seqr with 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
Source File: views.py    From lexpredict-contraxsuite with 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 #3
Source File: tests.py    From djongo with 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 #4
Source File: tests.py    From djongo with 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 #5
Source File: forms.py    From Spirit with 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 #6
Source File: tests.py    From djongo with 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 #7
Source File: prefetch.py    From dynamic-rest with 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 #8
Source File: prefetch.py    From dynamic-rest with 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 #9
Source File: views.py    From SchoolIdolAPI with 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 #10
Source File: tests.py    From djongo with 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 #11
Source File: views.py    From osler with 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 #12
Source File: RepositoryStatus.py    From civet with 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 #13
Source File: tests.py    From djongo with 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 #14
Source File: EventsStatus.py    From civet with 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 #15
Source File: profiles.py    From pasportaservo with 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 #16
Source File: popolo_extra.py    From yournextrepresentative with 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 #17
Source File: tests.py    From djongo with 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 #18
Source File: serializers.py    From instiapp-api with 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 #19
Source File: tests.py    From djongo with 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 #20
Source File: query.py    From graphene-django-optimizer with 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 #21
Source File: views.py    From PrivacyScore with 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 #22
Source File: tests.py    From djongo with 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 #23
Source File: orm_to_json_utils.py    From seqr with 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 #24
Source File: test_utils.py    From graphene-django-optimizer with MIT License 5 votes vote down vote up
def assert_query_equality(left_query, right_query):
    assert str(left_query.query) == str(right_query.query)
    assert len(left_query._prefetch_related_lookups) == len(right_query._prefetch_related_lookups)
    for (i, lookup) in enumerate(left_query._prefetch_related_lookups):
        right_lookup = right_query._prefetch_related_lookups[i]
        if isinstance(lookup, Prefetch) and isinstance(right_lookup, Prefetch):
            assert_query_equality(lookup.queryset, right_lookup.queryset)
        elif isinstance(lookup, Prefetch):
            assert str(lookup.queryset.query) == right_lookup
        elif isinstance(right_lookup, Prefetch):
            assert lookup == str(right_lookup.queryset.query)
        else:
            assert lookup == right_lookup 
Example #25
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_pickle_prefetch_queryset_still_usable(self):
        g = Group.objects.create(name='foo')
        groups = Group.objects.prefetch_related(
            models.Prefetch('event_set', queryset=Event.objects.order_by('id'))
        )
        groups2 = pickle.loads(pickle.dumps(groups))
        self.assertSequenceEqual(groups2.filter(id__gte=0), [g]) 
Example #26
Source File: query.py    From graphene-django-optimizer with MIT License 5 votes vote down vote up
def prefetch_related(self, name, store, queryset):
        if store.select_list or store.only_list:
            queryset = store.optimize_queryset(queryset)
            self.prefetch_list.append(Prefetch(name, queryset=queryset))
        elif store.prefetch_list:
            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)
        else:
            self.prefetch_list.append(name) 
Example #27
Source File: types.py    From graphene-django-plus with MIT License 5 votes vote down vote up
def get_queryset(cls, qs, info):
        """Get the queryset checking for permissions and optimizing the query.

        Override the default graphene's `get_queryset` to check for permissions
        and optimize the query performance.

        Note that the query will only be automaticallu optimized if,
        `graphene_django_optimizer` is installed.
        """
        if not cls.check_permissions(info.context.user):
            raise PermissionDenied("No permissions")

        if isinstance(qs, models.Manager):
            qs = qs.get_queryset()

        if (cls._meta.object_permissions and
                isinstance(cls._meta.model.objects, GuardedModelManager)):
            qs &= cls._meta.model.objects.for_user(
                info.context.user,
                cls._meta.object_permissions,
                any_perm=cls._meta.object_permissions_any,
            )

        ret = super().get_queryset(qs, info)
        if gql_optimizer is None:
            return ret

        ret = gql_optimizer.query(ret, info)
        prl = {i.to_attr if isinstance(i, Prefetch) else i: i
               for i in ret._prefetch_related_lookups}
        ret._prefetch_related_lookups = tuple(prl.values())

        return ret 
Example #28
Source File: areas.py    From yournextrepresentative with GNU Affero General Public License v3.0 5 votes vote down vote up
def get_context_data(self, **kwargs):
        context = super(AreasOfTypeView, self).get_context_data(**kwargs)
        requested_area_type = kwargs['area_type']
        all_area_tuples = set(
            (area_type.name, election_data.area_generation)
            for election_data in Election.objects.current().by_date()
            for area_type in election_data.area_types.all()
            if area_type.name == requested_area_type
        )
        if not all_area_tuples:
            raise Http404(_("Area '{0}' not found").format(requested_area_type))
        if len(all_area_tuples) > 1:
            message = _("Multiple Area generations for type {area_type} found")
            raise Exception(message.format(area_type=requested_area_type))
        prefetch_qs = AreaExtra.objects.select_related('base').order_by('base__name')
        area_type = get_object_or_404(
            AreaType.objects \
                .prefetch_related(Prefetch('areas', queryset=prefetch_qs)),
            name=requested_area_type
        )
        areas = [
            (
                reverse(
                    'areas-view',
                    kwargs={
                        'type_and_area_ids': '{type}-{area_id}'.format(
                            type=requested_area_type,
                            area_id=area.base.identifier
                        ),
                        'ignored_slug': slugify(area.base.name)
                    }
                ),
                area.base.name,
                requested_area_type,
            )
            for area in area_type.areas.all()
        ]
        context['areas'] = areas
        context['area_type'] = area_type
        return context 
Example #29
Source File: schema.py    From graphene-django-optimizer with MIT License 5 votes vote down vote up
def _prefetch_children(info, filter_input):
    if filter_input is None:
        filter_input = {}

    gte = filter_input.get('value', {}).get('gte', 0)
    return Prefetch(
        'children',
        queryset=gql_optimizer.query(Item.objects.filter(value__gte=int(gte)), info),
        to_attr='gql_custom_filtered_children',
    ) 
Example #30
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_pickle_prefetch_queryset_not_evaluated(self):
        Group.objects.create(name='foo')
        groups = Group.objects.prefetch_related(
            models.Prefetch('event_set', queryset=Event.objects.order_by('id'))
        )
        list(groups)  # evaluate QuerySet
        with self.assertNumQueries(0):
            pickle.loads(pickle.dumps(groups))