Python peewee.SelectQuery() Examples

The following are 10 code examples of peewee.SelectQuery(). 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 peewee , or try the search function .
Example #1
Source File: fields.py    From Quiver-alfred with MIT License 6 votes vote down vote up
def add(self, value, clear_existing=False):
        if clear_existing:
            self.clear()

        fd = self._field_descriptor
        if isinstance(value, SelectQuery):
            query = value.select(
                SQL(str(self._instance.get_id())),
                fd.rel_model._meta.primary_key)
            fd.through_model.insert_from(
                fields=[fd.src_fk, fd.dest_fk],
                query=query).execute()
        else:
            if not isinstance(value, (list, tuple)):
                value = [value]
            if not value:
                return
            inserts = [{
                fd.src_fk.name: self._instance.get_id(),
                fd.dest_fk.name: rel_id}
                for rel_id in self._id_list(value)]
            fd.through_model.insert_many(inserts).execute() 
Example #2
Source File: fields.py    From Quiver-alfred with MIT License 6 votes vote down vote up
def remove(self, value):
        fd = self._field_descriptor
        if isinstance(value, SelectQuery):
            subquery = value.select(value.model_class._meta.primary_key)
            return (fd.through_model
                    .delete()
                    .where(
                        (fd.dest_fk << subquery) &
                        (fd.src_fk == self._instance.get_id()))
                    .execute())
        else:
            if not isinstance(value, (list, tuple)):
                value = [value]
            if not value:
                return
            return (fd.through_model
                    .delete()
                    .where(
                        (fd.dest_fk << self._id_list(value)) &
                        (fd.src_fk == self._instance.get_id()))
                    .execute()) 
Example #3
Source File: gfk.py    From Quiver-alfred with MIT License 6 votes vote down vote up
def __set__(self, instance, value):
        mtv = instance._meta.db_table
        miv = instance._get_pk_value()
        if (isinstance(value, SelectQuery) and
                value.model_class == self.model_class):
            UpdateQuery(self.model_class, {
                self.model_type_field: mtv,
                self.model_id_field: miv,
            }).where(value._where).execute()
        elif all(map(lambda i: isinstance(i, self.model_class), value)):
            for obj in value:
                setattr(obj, self.model_type_field.name, mtv)
                setattr(obj, self.model_id_field.name, miv)
                obj.save()
        else:
            raise ValueError('ReverseGFK field unable to handle "%s"' % value) 
Example #4
Source File: peewee.py    From nplusone with MIT License 6 votes vote down vote up
def query_execute(self, database):
    ret = original_query_execute(self, database)
    if hasattr(self, '_context'):
        # Query has been marked as lazy during backref lookup
        signals.lazy_load.send(
            signals.get_worker(),
            args=self._context['args'],
            kwargs=self._context['kwargs'],
            parser=parse_reverse_get,
        )
    if not isinstance(self, SelectQuery):
        return ret
    signal = (
        signals.ignore_load
        if is_single(self._offset, self._limit)
        else signals.load
    )
    signal.send(
        signals.get_worker(),
        args=(self, ),
        ret=list(ret),
        parser=parse_load,
    )
    return ret 
Example #5
Source File: peewee_async.py    From peewee-async with MIT License 6 votes vote down vote up
def select(query):
    """Perform SELECT query asynchronously.
    """
    assert isinstance(query, peewee.SelectQuery),\
        ("Error, trying to run select coroutine"
         "with wrong query class %s" % str(query))

    cursor = await _execute_query_async(query)

    result = AsyncQueryWrapper(cursor=cursor, query=query)

    try:
        while True:
            await result.fetchone()
    except GeneratorExit:
        pass
    finally:
        await cursor.release()

    return result 
Example #6
Source File: fields.py    From aiopeewee with MIT License 6 votes vote down vote up
def add(self, value, clear_existing=False):
        if clear_existing:
            await self.clear()

        fd = self._field_descriptor
        if isinstance(value, SelectQuery):
            query = value.select(
                SQL(str(self._instance.get_id())),
                fd.rel_model._meta.primary_key)
            await fd.through_model.insert_from(
                fields=[fd.src_fk, fd.dest_fk],
                query=query).execute()
        else:
            if not isinstance(value, (list, tuple)):
                value = [value]
            if not value:
                return
            inserts = [{
                fd.src_fk.name: self._instance.get_id(),
                fd.dest_fk.name: rel_id}
                for rel_id in self._id_list(value)]
            await fd.through_model.insert_many(inserts).execute() 
Example #7
Source File: fields.py    From aiopeewee with MIT License 6 votes vote down vote up
def remove(self, value):
        fd = self._field_descriptor
        if isinstance(value, SelectQuery):
            subquery = value.select(value.model_class._meta.primary_key)
            return await (fd.through_model
                          .delete()
                          .where(
                (fd.dest_fk << subquery) &
                (fd.src_fk == self._instance.get_id()))
                          .execute())
        else:
            if not isinstance(value, (list, tuple)):
                value = [value]
            if not value:
                return
            return await (fd.through_model
                          .delete()
                          .where(
                (fd.dest_fk << self._id_list(value)) &
                (fd.src_fk == self._instance.get_id()))
                          .execute()) 
Example #8
Source File: flask_utils.py    From Quiver-alfred with MIT License 5 votes vote down vote up
def __init__(self, query_or_model, paginate_by, page_var='page',
                 check_bounds=False):
        self.paginate_by = paginate_by
        self.page_var = page_var
        self.check_bounds = check_bounds

        if isinstance(query_or_model, SelectQuery):
            self.query = query_or_model
            self.model = self.query.model_class
        else:
            self.model = query_or_model
            self.query = self.model.select() 
Example #9
Source File: flask_utils.py    From Quiver-alfred with MIT License 5 votes vote down vote up
def get_object_or_404(query_or_model, *query):
    if not isinstance(query_or_model, SelectQuery):
        query_or_model = query_or_model.select()
    try:
        return query_or_model.where(*query).get()
    except DoesNotExist:
        abort(404) 
Example #10
Source File: webutil.py    From restpie3 with MIT License 5 votes vote down vote up
def default(self, obj):
        if isinstance(obj, peewee.SelectQuery):
            return list(obj)
        if isinstance(obj, db.BaseModel):
            return obj.serialize()
        elif isinstance(obj, datetime.datetime):
#             dt_local = util.utc2local(obj)
            return obj.isoformat() if obj else None
        return JSONEncoder.default(self, obj)