Opened 16 years ago

Closed 16 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


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(, op)
        param = str(getattr(self, field.attname))
        q = self.__class__._default_manager.filter(**kwargs).order_by((not is_next and '-' or '') +, (not is_next and '-' or '') +
        q._params.extend([param, param, getattr(self,])
            while q[i] == self:
            return q[i]
        except IndexError:
            raise self.DoesNotExist, "%s matching query does not exist." % self.__class__._meta.object_name

I changed

return q[0]


            while q[i] == self:
            return q[i]

Change History (1)

comment:1 Changed 16 years ago by Pete Crosier

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