def get(self, id=None, alias=None, **kwargs): ''' Gets a single item of the current queryset collection using it's id. In order to query a different database, please specify the `alias` of the database to query. ''' from motorengine.aiomotorengine import Q if id is None and not kwargs: raise RuntimeError( "Either an id or a filter must be provided to get") if id is not None: if not isinstance(id, ObjectId): id = ObjectId(id) filters = {"_id": id} else: filters = Q(**kwargs) filters = self.get_query_from_filters(filters) instance = yield from self.coll(alias).find_one( filters, projection=self._loaded_fields.to_query(self.__klass__)) if instance is None: return None else: doc = self.__klass__.from_son( instance, # if _loaded_fields is not empty then # document is partly loaded _is_partly_loaded=bool(self._loaded_fields), # set projections for references (if any) _reference_loaded_fields=self._reference_loaded_fields) if self.is_lazy: return doc else: yield from doc.load_references() return doc
def test_gets_proper_type(self): query = Q(first_name="Test") expect(query).to_be_instance_of(Q) query = Q(first_name="Test") | Q(first_name="Else") expect(query).to_be_instance_of(QCombination)