Code

Opened 7 years ago

Closed 6 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: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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]

Attachments (0)

Change History (1)

comment:1 Changed 6 years ago by PJCrosier

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to worksforme
  • Status changed from new to closed

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.