get_{next,previous}_by_someDateField()

103 views
Skip to first unread message

Marc Fargas

unread,
Jun 10, 2008, 5:26:14 AM6/10/08
to django-d...@googlegroups.com
Hi,

There are two things about get_NEXT/PREVIOUS_by_DATEFIELD() which I'm
not sure if a bug should be filled agains (almost sure anyway).

given a simple model like:

class TestModel(models.Model):
date = models.DateTimeField(auto_now_add=True)

m = TestModel()
m.get_previous_by_date()

Will fail with an exception which says "Cannot use None as a query",
maybe that error could be a bit more self-explicative, like "Cannot
query next/previous items without being saved".

Which brings up the other issue, when previous/next is called the
resulting query that Django will use includes two filters: WHERE (date <
self.date) OR (date < self.date AND id < self.id). I don't get the
reason for the second filter, but this makes next/prev unusable with
unsaved objects.

Is this second filter really required? Or could it be either:
* Removed
* Added only when the object has an id

In any case, the error when using both function on unsaved objects could
be a bit more explicative. I'll fill tickets and provide patches
according to your comments, so please, comment :)

Thanks,
Marc


--
http://www.marcfargas.com -- will be finished some day.

signature.asc

Karen Tracey

unread,
Jun 10, 2008, 9:54:24 AM6/10/08
to django-d...@googlegroups.com
On Tue, Jun 10, 2008 at 5:26 AM, Marc Fargas <tele...@telenieko.com> wrote:
Hi,

There are two things about get_NEXT/PREVIOUS_by_DATEFIELD() which I'm
not sure if a bug should be filled agains (almost sure anyway).

given a simple model like:

       class TestModel(models.Model):
               date = models.DateTimeField(auto_now_add=True)

m = TestModel()
m.get_previous_by_date()

Will fail with an exception which says "Cannot use None as a query",
maybe that error could be a bit more self-explicative, like "Cannot
query next/previous items without being saved".

Which brings up the other issue, when previous/next is called the
resulting query that Django will use includes two filters: WHERE (date <
self.date) OR (date < self.date AND id < self.id). I don't get the
reason for the second filter, but this makes next/prev unusable with
unsaved objects.

Is this second filter really required? Or could it be either:
       * Removed
       * Added only when the object has an id

The ID condition is to handle the case where multiple records have the same date value.  From:

http://www.djangoproject.com/documentation/db-api/#get-next-by-foo-kwargs-and-get-previous-by-foo-kwargs

"Note that in the case of identical date values, these methods will use the ID as a fallback check. This guarantees that no records are skipped or duplicated. For a full example, see the lookup API sample model."

Allowing you to start with an unsaved object would introduce its own oddity.  You could get (or impose) an ordering between your one unsaved object you are starting with and all the records saved in the DB, but any other unsaved objects you have are excluded from the ordering, since they're not in the DB to be found.  Personally I think it's cleaner to require a saved object for these functions.

In any case, the error when using both function on unsaved objects could
be a bit more explicative. I'll fill tickets and provide patches
according to your comments, so please, comment :)

I agree the error message could be better.

Karen

Gary Wilson Jr.

unread,
Jun 11, 2008, 2:31:25 PM6/11/08
to django-d...@googlegroups.com
Marc Fargas wrote:
> m = TestModel()
> m.get_previous_by_date()
>
> Will fail with an exception which says "Cannot use None as a query",
> maybe that error could be a bit more self-explicative, like "Cannot
> query next/previous items without being saved".

Agreed, please create a ticket for this.

Gary

Marc Fargas

unread,
Jun 12, 2008, 10:49:21 AM6/12/08
to django-d...@googlegroups.com
El mar, 10-06-2008 a las 09:54 -0400, Karen Tracey escribió:


> I agree the error message could be better.
>

Thanks for the elaborate and concise answer, I'll work on a nicer error
message then ;)

Should the error raise a ProgrammingError or ValueError ?

signature.asc
Reply all
Reply to author
Forward
0 new messages