Opened 17 years ago

Closed 17 years ago

#5845 closed (worksforme)

_get_next_or_previous_by_FIELD returns the same row in first row

Reported by: psclil+django@… Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I'm using sqlite3

when i use:

next = post.get_next_by_date_added()

if post is the first date:

next becomes equale to post.

otherwise this works fine...

i didn't want to change the sql generated or something like that because i'm really new to the framework,
so i decided to do little fixing before returning a value:

    def _get_next_or_previous_by_FIELD(self, field, is_next, **kwargs):
        op = is_next and '>' or '<'
        where = '(%s %s %%s OR (%s = %%s AND %s.%s %s %%s))' % \
            (backend.quote_name(field.column), op, backend.quote_name(field.column),
            backend.quote_name(self._meta.db_table), backend.quote_name(self._meta.pk.column), op)
        param = str(getattr(self, field.attname))
        q = self.__class__._default_manager.filter(**kwargs).order_by((not is_next and '-' or '') + field.name, (not is_next and '-' or '') + self._meta.pk.name)
        q._where.append(where)
        q._params.extend([param, param, getattr(self, self._meta.pk.attname)])
        try:
            i=0
            while q[i] == self:
                i=i+1
            return q[i]
        except IndexError:
            raise self.DoesNotExist, "%s matching query does not exist." % self.__class__._meta.object_name

I changed

return q[0]

into

            i=0
            while q[i] == self:
                i=i+1
            return q[i]

Change History (1)

comment:1 by Pete Crosier, 17 years ago

Resolution: worksforme
Status: newclosed

I'm going to be bold and close this because I've been trying hard to break it and I can't, behaviour is as expected on SQLite with the latest trunk. Please re-open if you're still experiencing this and you can be a bit more explicit about how we can replicate it.

Note: See TracTickets for help on using tickets.
Back to Top