Python django.db.models.query.QuerySet() Examples
The following are 30
code examples of django.db.models.query.QuerySet().
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.query
, or try the search function
.
Example #1
Source File: test_aws_report_db_accessor.py From koku with GNU Affero General Public License v3.0 | 6 votes |
def test_get_db_obj_query_with_columns(self): """Test that a query is returned with limited columns.""" table_name = random.choice(self.foreign_key_tables) columns = list(REPORT_COLUMN_MAP[table_name].values()) selected_columns = [random.choice(columns) for _ in range(2)] missing_columns = set(columns).difference(selected_columns) query = self.accessor._get_db_obj_query(table_name, columns=selected_columns) with schema_context(self.schema): self.assertIsInstance(query, QuerySet) result = query.first() for column in selected_columns: self.assertTrue(column in result) for column in missing_columns: self.assertFalse(column in result)
Example #2
Source File: models.py From elasticsearch-django with MIT License | 6 votes |
def get_search_queryset(self, index: str = "_all") -> QuerySet: """ Return the dataset used to populate the search index. Kwargs: index: string, the name of the index we are interested in - this allows us to have different sets of objects in different indexes. Defaults to '_all', in which case all indexes index the same set of objects. This must return a queryset object. """ raise NotImplementedError( "{} does not implement 'get_search_queryset'.".format( self.__class__.__name__ ) )
Example #3
Source File: test_shortcuts.py From resolwe with Apache License 2.0 | 6 votes |
def test_multi_perms_no_groups(self): group_names = ["group1", "group2", "group3"] groups = [Group.objects.create(name=name) for name in group_names] for group in groups: assign_perm("auth.change_group", self.contributor, group) assign_perm("auth.delete_group", self.contributor, groups[1]) objects = get_objects_for_user( self.contributor, ["auth.change_group", "auth.delete_group"], use_groups=False, ) self.assertEqual(len(objects), 1) self.assertTrue(isinstance(objects, QuerySet)) self.assertEqual( set(objects.values_list("name", flat=True)), set([groups[1].name]) )
Example #4
Source File: shortcuts.py From GTDWeb with GNU General Public License v2.0 | 6 votes |
def get_object_or_404(klass, *args, **kwargs): """ Uses get() to return an object, or raises a Http404 exception if the object does not exist. klass may be a Model, Manager, or QuerySet object. All other passed arguments and keyword arguments are used in the get() query. Note: Like with get(), an MultipleObjectsReturned will be raised if more than one object is found. """ queryset = _get_queryset(klass) try: return queryset.get(*args, **kwargs) except queryset.model.DoesNotExist: raise Http404('No %s matches the given query.' % queryset.model._meta.object_name)
Example #5
Source File: mixins.py From DjangoRestMultipleModels with MIT License | 6 votes |
def load_queryset(self, query_data, request, *args, **kwargs): """ Fetches the queryset and runs any necessary filtering, both built-in rest_framework filters and custom filters passed into the querylist """ queryset = query_data.get('queryset', []) if isinstance(queryset, QuerySet): # Ensure queryset is re-evaluated on each request. queryset = queryset.all() # run rest_framework filters queryset = self.filter_queryset(queryset) # run custom filters filter_fn = query_data.get('filter_fn', None) if filter_fn is not None: queryset = filter_fn(queryset, request, *args, **kwargs) page = self.paginate_queryset(queryset) self.is_paginated = page is not None return page if page is not None else queryset
Example #6
Source File: task_utils.py From lexpredict-contraxsuite with GNU Affero General Public License v3.0 | 6 votes |
def normalize(task_id, key, value): try: json.dumps(value) return value except TypeError: if isinstance(value, models.Model): return SimpleObjectSerializer().serialize([value]).pop() elif isinstance(value, QuerySet): return SimpleObjectSerializer().serialize(value) elif isinstance(value, (dict, list, tuple, set)): return pre_serialize(task_id, key, value) elif isinstance(value, UploadedFile): uploaded_file = value # type: UploadedFile cache_key = str(task_id) + '__' + str(key) if key else str(task_id) DbCache.put_to_db(cache_key, uploaded_file.read()) return { 'file_name': uploaded_file.name, 'cache_key': cache_key } return str(value)
Example #7
Source File: test_ocp_report_db_accessor.py From koku with GNU Affero General Public License v3.0 | 6 votes |
def test_get_db_obj_query_with_columns(self): """Test that a query is returned with limited columns.""" table_name = OCP_REPORT_TABLE_MAP["line_item"] columns = list(REPORT_COLUMN_MAP[table_name].values()) selected_columns = [random.choice(columns) for _ in range(2)] missing_columns = set(columns).difference(selected_columns) query = self.accessor._get_db_obj_query(table_name, columns=selected_columns) self.assertIsInstance(query, QuerySet) with schema_context(self.schema): result = query.first() for column in selected_columns: self.assertTrue(column in result) for column in missing_columns: self.assertFalse(column in result)
Example #8
Source File: layer_selection.py From urbanfootprint with GNU General Public License v3.0 | 6 votes |
def update_summary_results(self, query_result): """ Updates the summary results with the given QuerySet or results list :param self: :param query_result: :return: """ if isinstance(query_result, QuerySet): # Find aggregate and normal field names aggregate_names = query_result.aggregate_names if hasattr(query_result, 'aggregate_names') else [] self.summary_fields = (query_result.field_names if hasattr(query_result, 'field_names') else []) + aggregate_names # Find aggregate and normal field titles aggregate_titles = map_dict(lambda key, value: self.cleanup_title(key), query_result.query.aggregates) if hasattr(query_result.query, 'aggregates') else [] titles = map(lambda tup: self.cleanup_title(tup[1]), query_result.query.select) + aggregate_titles # Create a lookup from field name to title self.summary_field_title_lookup = dual_map_to_dict(lambda key, value: [key, value], self.summary_fields, titles) self.summary_query_sql = str(query_result.query) elif len(query_result) > 0: # For single row aggregates. TODO figure out who to extract the names from the query self.summary_fields = query_result[0].keys() self.summary_field_title_lookup = map_to_dict(lambda key: [key, key], self.summary_fields) self.summary_query_sql = str(query_result.query) self.summary_results = list(query_result) self.save()
Example #9
Source File: layer_selection.py From urbanfootprint with GNU General Public License v3.0 | 6 votes |
def update_features(self, query_set): """ Updates the features property ManyToMany with the features returned by self.create_query_set, whose QuerySet is based upon the current values of the LayerSelection instance :param query_set: The query_set from create_query_set or similar :return: """ self.clear_features() if not query_set: # If none keep features clear and return return # Update the features based on the new query_set self.features.through.objects.bulk_create( map( lambda feature: self.features.through( feature=feature, layer_selection=self), query_set.all() ) )
Example #10
Source File: response.py From django-excel-response with Apache License 2.0 | 6 votes |
def content(self, value): workbook = None if not bool(value) or not len(value): # Short-circuit to protect against empty querysets/empty lists/None, etc self._container = [] return elif isinstance(value, list): workbook = self._serialize_list(value) elif isinstance(value, QuerySet): workbook = self._serialize_queryset(value) if django.VERSION < (1, 9): if isinstance(value, ValuesQuerySet): workbook = self._serialize_values_queryset(value) if workbook is None: raise ValueError('ExcelResponse accepts the following data types: list, dict, QuerySet, ValuesQuerySet') if self.force_csv: self['Content-Type'] = 'text/csv; charset=utf8' self['Content-Disposition'] = 'attachment;filename="{}.csv"'.format(self.output_filename) workbook.seek(0) workbook = self.make_bytes(workbook.getvalue()) else: self['Content-Type'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' self['Content-Disposition'] = 'attachment; filename="{}.xlsx"'.format(self.output_filename) workbook = save_virtual_workbook(workbook) self._container = [self.make_bytes(workbook)]
Example #11
Source File: fields.py From django-spillway with BSD 3-Clause "New" or "Revised" License | 6 votes |
def bind(self, field_name, parent): try: renderer = parent.context['request'].accepted_renderer except (AttributeError, KeyError): pass else: obj = parent.root.instance try: has_format = renderer.format in obj.query.annotations except AttributeError: if not isinstance(obj, QuerySet): try: obj = obj[0] except (IndexError, TypeError): pass has_format = hasattr(obj, renderer.format) if has_format: self.source = renderer.format super(GeometryField, self).bind(field_name, parent)
Example #12
Source File: list.py From django-telegram-bot with BSD 3-Clause "New" or "Revised" License | 6 votes |
def get_queryset(self): if self.queryset is not None: queryset = self.queryset if isinstance(queryset, QuerySet): queryset = queryset.all() elif self.model is not None: queryset = self.model._default_manager.all() else: raise ImproperlyConfigured( "%(cls)s is missing a QuerySet. Define " "%(cls)s.model, %(cls)s.queryset, or override " "%(cls)s.get_queryset()." % { 'cls': self.__class__.__name__ } ) ordering = self.get_ordering() if ordering: if isinstance(ordering, six.string_types): ordering = (ordering,) queryset = queryset.order_by(*ordering) return queryset
Example #13
Source File: models.py From zulip with Apache License 2.0 | 6 votes |
def query_for_ids(query: QuerySet, user_ids: List[int], field: str) -> QuerySet: ''' This function optimizes searches of the form `user_profile_id in (1, 2, 3, 4)` by quickly building the where clauses. Profiling shows significant speedups over the normal Django-based approach. Use this very carefully! Also, the caller should guard against empty lists of user_ids. ''' assert(user_ids) clause = f'{field} IN %s' query = query.extra( where=[clause], params=(tuple(user_ids),), ) return query # Doing 1000 remote cache requests to get_display_recipient is quite slow, # so add a local cache as well as the remote cache cache. # # This local cache has a lifetime of just a single request; it is # cleared inside `flush_per_request_caches` in our middleware. It # could be replaced with smarter bulk-fetching logic that deduplicates # queries for the same recipient; this is just a convenient way to # write that code.
Example #14
Source File: views.py From zulip with Apache License 2.0 | 6 votes |
def raw_user_activity_table(records: List[QuerySet]) -> str: cols = [ 'query', 'client', 'count', 'last_visit', ] def row(record: QuerySet) -> List[Any]: return [ record.query, record.client.name, record.count, format_date_for_activity_reports(record.last_visit), ] rows = list(map(row, records)) title = 'Raw Data' return make_table(title, cols, rows)
Example #15
Source File: models.py From zulip with Apache License 2.0 | 6 votes |
def get_users_by_delivery_email(emails: Set[str], realm: Realm) -> QuerySet: """This is similar to get_users_by_delivery_email, and it has the same security caveats. It gets multiple users and returns a QuerySet, since most callers will only need two or three fields. If you are using this to get large UserProfile objects, you are probably making a mistake, but if you must, then use `select_related`. """ ''' Django doesn't support delivery_email__iexact__in, so we simply OR all the filters that we'd do for the one-email case. ''' email_filter = Q() for email in emails: email_filter |= Q(delivery_email__iexact=email.strip()) return UserProfile.objects.filter(realm=realm).filter(email_filter)
Example #16
Source File: users.py From zulip with Apache License 2.0 | 6 votes |
def user_profile_to_user_row(user_profile: UserProfile) -> Dict[str, Any]: # What we're trying to do is simulate the user_profile having been # fetched from a QuerySet using `.values(*realm_user_dict_fields)` # even though we fetched UserProfile objects. This is messier # than it seems. # # What we'd like to do is just call model_to_dict(user, # fields=realm_user_dict_fields). The problem with this is # that model_to_dict has a different convention than # `.values()` in its handling of foreign keys, naming them as # e.g. `bot_owner`, not `bot_owner_id`; we work around that # here. # # This could be potentially simplified in the future by # changing realm_user_dict_fields to name the bot owner with # the less readable `bot_owner` (instead of `bot_owner_id`). user_row = model_to_dict(user_profile, fields=realm_user_dict_fields + ['bot_owner']) user_row['bot_owner_id'] = user_row['bot_owner'] del user_row['bot_owner'] return user_row
Example #17
Source File: views.py From zulip with Apache License 2.0 | 6 votes |
def get_user_activity_records_for_realm(realm: str, is_bot: bool) -> QuerySet: fields = [ 'user_profile__full_name', 'user_profile__delivery_email', 'query', 'client__name', 'count', 'last_visit', ] records = UserActivity.objects.filter( user_profile__realm__string_id=realm, user_profile__is_active=True, user_profile__is_bot=is_bot, ) records = records.order_by("user_profile__delivery_email", "-last_visit") records = records.select_related('user_profile', 'client').only(*fields) return records
Example #18
Source File: layer_selection.py From urbanfootprint with GNU General Public License v3.0 | 5 votes |
def sync_to_query(self): """ Updates the query_set and related derived values based on the values of the query parameters """ feature_class = self.feature_class query_set = self.create_query_set(feature_class.objects) # Set the Features to the distinct set. Join queries can result in duplicates of the main model self.update_features(query_set and query_set.distinct('pk')) # Parse the QuerySet to get the result fields and their column title lookup # This will give us the fields of the main model, and if we have joins those of the related models regular_query_set = query_set or feature_class.objects # Create the values query set if there are joins to handle. # If not create the values query set based on the regular query set # This function has the side-effect of saving and storing the result_map # In the latter case we are just doing calling the function to create the result_map values_query_set = self.values_query_set() if\ self.joins and len(self.joins) > 0 else\ self.values_query_set(regular_query_set) # Save the query for use by the exporter and for debugging self.query_sql = str(values_query_set.query) # Create the summary results from the entire set summary_query_set = self.sync_summary_query_set(feature_class.objects) if summary_query_set: # Update the summary results self.update_summary_results(summary_query_set) else: self.clear_summary_results()
Example #19
Source File: utils.py From opensurfaces with MIT License | 5 votes |
def progress_bar(l, show_progress=True): """ Returns an iterator for a list or queryset that renders a progress bar with a countdown timer """ if show_progress: if isinstance(l, QuerySet): return queryset_progress_bar(l) else: return progress.bar(l) else: return l
Example #20
Source File: view_tests.py From codesy with GNU Affero General Public License v3.0 | 5 votes |
def test_attrs(self): self.assertIsInstance( self.viewset, views.AutoOwnObjectsModelViewSet) self.assertIsInstance(self.viewset.queryset, QuerySet) self.assertEqual(self.viewset.queryset.model, models.Vote) self.assertEqual( self.viewset.serializer_class, serializers.VoteSerializer)
Example #21
Source File: view_tests.py From codesy with GNU Affero General Public License v3.0 | 5 votes |
def test_attrs(self): self.assertIsInstance( self.viewset, views.AutoOwnObjectsModelViewSet) self.assertIsInstance(self.viewset.queryset, QuerySet) self.assertEqual(self.viewset.queryset.model, models.Claim) self.assertEqual( self.viewset.serializer_class, serializers.ClaimSerializer)
Example #22
Source File: view_tests.py From codesy with GNU Affero General Public License v3.0 | 5 votes |
def test_attrs(self): self.assertIsInstance( self.viewset, views.AutoOwnObjectsModelViewSet) self.assertIsInstance(self.viewset.queryset, QuerySet) self.assertEqual(self.viewset.queryset.model, models.Bid) self.assertEqual( self.viewset.serializer_class, serializers.BidSerializer)
Example #23
Source File: models.py From django-project with BSD 3-Clause "New" or "Revised" License | 5 votes |
def fname(self, model_or_obj_or_qs): """ Return the field name on the :class:`Follow` model for ``model_or_obj_or_qs``. """ if isinstance(model_or_obj_or_qs, QuerySet): _, fname = model_map[model_or_obj_or_qs.model] else: cls = model_or_obj_or_qs if inspect.isclass(model_or_obj_or_qs) else model_or_obj_or_qs.__class__ _, fname = model_map[cls] return fname
Example #24
Source File: test_params.py From django-click with MIT License | 5 votes |
def test_modelinstance_init(): from testapp.models import DummyModel from django.db.models.query import QuerySet param = params.ModelInstance(DummyModel) assert isinstance(param.qs, QuerySet) qs = DummyModel.objects.all() param = params.ModelInstance(qs) assert param.qs is qs
Example #25
Source File: views.py From zulip with Apache License 2.0 | 5 votes |
def get_user_activity_records_for_email(email: str) -> List[QuerySet]: fields = [ 'user_profile__full_name', 'query', 'client__name', 'count', 'last_visit', ] records = UserActivity.objects.filter( user_profile__delivery_email=email, ) records = records.order_by("-last_visit") records = records.select_related('user_profile', 'client').only(*fields) return records
Example #26
Source File: models.py From django-project with BSD 3-Clause "New" or "Revised" License | 5 votes |
def get_follows(self, model_or_obj_or_qs): """ Returns all the followers of a model, an object or a queryset. """ fname = self.fname(model_or_obj_or_qs) if isinstance(model_or_obj_or_qs, QuerySet): return self.filter(**{'%s__in' % fname: model_or_obj_or_qs}) if inspect.isclass(model_or_obj_or_qs): return self.exclude(**{fname:None}) return self.filter(**{fname:model_or_obj_or_qs})
Example #27
Source File: topic.py From zulip with Apache License 2.0 | 5 votes |
def messages_for_topic(stream_recipient_id: int, topic_name: str) -> QuerySet: return Message.objects.filter( recipient_id=stream_recipient_id, subject__iexact=topic_name, )
Example #28
Source File: topic.py From zulip with Apache License 2.0 | 5 votes |
def filter_by_topic_name_via_message(query: QuerySet, topic_name: str) -> QuerySet: return query.filter(message__subject__iexact=topic_name)
Example #29
Source File: topic.py From zulip with Apache License 2.0 | 5 votes |
def filter_by_exact_message_topic(query: QuerySet, message: Message) -> QuerySet: topic_name = message.topic_name() return query.filter(subject=topic_name)
Example #30
Source File: ra_tags.py From django-ra-erp with GNU Affero General Public License v3.0 | 5 votes |
def jsonify(object): def date_handler(obj): if hasattr(obj, 'isoformat'): return obj.isoformat() elif isinstance(obj, Promise): return force_text(obj) # else: # raise TypeError( # "Unserializable object {} of type {}".format(obj, type(obj)) # ) if isinstance(object, QuerySet): return serialize('json', object) return mark_safe(json.dumps(object, use_decimal=True, default=date_handler))