On Tue, Jun 10, 2008 at 5:26 AM, Marc Fargas <[EMAIL PROTECTED]>
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

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to