コード例 #1
0
ファイル: query.py プロジェクト: abulte/python-stdnet
    def aggregate(self, kwargs):
        '''Aggregate lookup parameters.'''
        meta = self._meta
        fields = meta.dfields
        field_lookups = {}
        for name, value in iteritems(kwargs):
            bits = name.split(JSPLITTER)
            field_name = bits.pop(0)
            if field_name not in fields:
                raise QuerySetError('Could not filter on model "{0}".\
 Field "{1}" does not exist.'.format(meta, field_name))
            field = fields[field_name]
            attname = field.attname
            if bits:
                bits = [n.lower() for n in bits]
                if bits[-1] == 'in':
                    bits.pop()
                lookup = JSPLITTER.join(bits)
                if lookup and lookup not in range_lookups:
                    lvalue = field.filter(self.session, lookup, value)
                    if lvalue is not None:
                        value = lvalue
                        lookup = None
                    else:
                        if bits[-1] in range_lookups:
                            lookup = bits.pop()
                        else:
                            lookup = None
                        bits.insert(0, attname)
                        attname = JSPLITTER.join(bits)
            else:
                lookup = None
            # Get lookups on attribute name
            lookups = field_lookups.get(attname)
            if lookups is None:
                lookups = []
                field_lookups[attname] = lookups
            if lookup not in range_lookups:
                if not field.index:
                    raise QuerySetError("{0} {1} is not an index.\
 Cannot query.".format(field.__class__.__name__,field_name))
                if not iterable(value):
                    value = (value,)
                for v in value:
                    if isinstance(v, Q):
                        v = lookup_value('set', v.construct())
                    else:
                        v = lookup_value('value', field.dumps(v, lookup))
                    lookups.append(v)
            else:
                lookups.append(lookup_value(lookup, field.dumps(value, lookup)))
        return [queryset(self, name=name, underlying=field_lookups[name])\
                for name in sorted(field_lookups)]
コード例 #2
0
ファイル: query.py プロジェクト: jbking/python-stdnet
    def load_only(self, *fields):
        '''This is provides a :ref:`performance boost <increase-performance>`
in cases when you need to load a subset of fields of your model. The boost
achieved is less than the one obtained when using
:meth:`Query.load_related`, since it does not reduce the number of requests
to the database. However, it can save you lots of bandwidth when excluding
data intensive fields you don't need.
'''
        q = self._clone()
        new_fields = []
        for field in fields:
            if JSPLITTER in field:
                bits = field.split(JSPLITTER)
                related = self._get_related_field(bits[0])
                if related:
                    q._add_to_load_related(related, JSPLITTER.join(bits[1:]))
                    continue
            new_fields.append(field)
        if fields and not new_fields:
            # if we added a field to the load_related list and not fields are
            # are left we add the primary key so that other firls are not
            # loaded.
            new_fields.append(self._meta.pkname())
        fs = unique_tuple(q.fields, new_fields)
        q.data['fields'] = fs if fs else None
        return q
コード例 #3
0
    def load_only(self, *fields):
        '''This is provides a :ref:`performance boost <increase-performance>`
in cases when you need to load a subset of fields of your model. The boost
achieved is less than the one obtained when using
:meth:`Query.load_related`, since it does not reduce the number of requests
to the database. However, it can save you lots of bandwidth when excluding
data intensive fields you don't need.
'''
        q = self._clone()
        new_fields = []
        for field in fields:
            if JSPLITTER in field:
                bits = field.split(JSPLITTER)
                related = self._get_related_field(bits[0])
                if related:
                    q._add_to_load_related(related, JSPLITTER.join(bits[1:]))
                    continue
            new_fields.append(field)
        if fields and not new_fields:
            # if we added a field to the load_related list and not fields are
            # are left we add the primary key so that other firls are not
            # loaded.
            new_fields.append(self._meta.pkname())
        fs = unique_tuple(q.fields, new_fields)
        q.data['fields'] = fs if fs else None
        return q
コード例 #4
0
ファイル: query.py プロジェクト: suziwen/python-stdnet
    def aggregate(self, kwargs):
        """Aggregate lookup parameters."""
        meta = self._meta
        fields = meta.dfields
        field_lookups = {}
        for name, value in iteritems(kwargs):
            bits = name.split(JSPLITTER)
            field_name = bits.pop(0)
            if field_name not in fields:
                raise QuerySetError(
                    'Could not filter on model "{0}".\
 Field "{1}" does not exist.'.format(
                        meta, field_name
                    )
                )
            field = fields[field_name]
            attname = field.attname
            lookup = None
            if bits:
                bits = [n.lower() for n in bits]
                if bits[-1] == "in":
                    bits.pop()
                elif bits[-1] in range_lookups:
                    lookup = bits.pop()
                remaining = JSPLITTER.join(bits)
                if lookup:  # this is a range lookup
                    attname, nested = field.get_lookup(remaining, QuerySetError)
                    lookups = get_lookups(attname, field_lookups)
                    lookups.append(lookup_value(lookup, (value, nested)))
                    continue
                elif remaining:  # Not a range lookup, must be a nested filter
                    value = field.filter(self.session, remaining, value)
            lookups = get_lookups(attname, field_lookups)
            # If we are here the field must be an index
            if not field.index:
                raise QuerySetError("%s %s is not an index. Cannot query." % (field.__class__.__name__, field_name))
            if not iterable(value):
                value = (value,)
            for v in value:
                if isinstance(v, Q):
                    v = lookup_value("set", v.construct())
                else:
                    v = lookup_value("value", field.serialise(v, lookup))
                lookups.append(v)
        #
        return [queryset(self, name=name, underlying=field_lookups[name]) for name in sorted(field_lookups)]
コード例 #5
0
    def aggregate(self, kwargs):
        '''Aggregate lookup parameters.'''
        meta = self._meta
        fields = meta.dfields
        field_lookups = {}
        for name, value in iteritems(kwargs):
            bits = name.split(JSPLITTER)
            field_name = bits.pop(0)
            if field_name not in fields:
                raise QuerySetError('Could not filter on model "{0}".\
 Field "{1}" does not exist.'.format(meta, field_name))
            field = fields[field_name]
            attname = field.attname
            lookup = None
            if bits:
                bits = [n.lower() for n in bits]
                if bits[-1] == 'in':
                    bits.pop()
                elif bits[-1] in range_lookups:
                    lookup = bits.pop()
                remaining = JSPLITTER.join(bits)
                if lookup:  # this is a range lookup
                    attname, nested = field.get_lookup(remaining,
                                                       QuerySetError)
                    lookups = get_lookups(attname, field_lookups)
                    lookups.append(lookup_value(lookup, (value, nested)))
                    continue
                elif remaining:   # Not a range lookup, must be a nested filter
                    value = field.filter(self.session, remaining, value)
            lookups = get_lookups(attname, field_lookups)
            # If we are here the field must be an index
            if not field.index:
                raise QuerySetError("%s %s is not an index. Cannot query." %
                                    (field.__class__.__name__, field_name))
            if not iterable(value):
                value = (value,)
            for v in value:
                if isinstance(v, Q):
                    v = lookup_value('set', v.construct())
                else:
                    v = lookup_value('value', field.serialise(v, lookup))
                lookups.append(v)
        #
        return [queryset(self, name=name, underlying=field_lookups[name])
                for name in sorted(field_lookups)]