Hi all,

So I've got a query which contains a subquery. I would like to
paginate based on this subquery and I'm not sure how.

The pertinent code is as follows:


class Event(models.Model):
    description = models.CharField(max_length=200)
    date = models.DateTimeField()

    def __unicode__(self):
        return self.description


def index(request):
    from django.db import connection
    opts = Event._meta
    event_list = Event.objects.extra(where=["""
    DATE(%(event)s.%(date)s) IN
    (
      SELECT DISTINCT DATE(%(event)s.%(date)s)
      FROM %(event)s
      ORDER BY 1 DESC
      %(limit)s
    )
    ORDER BY DATE(%(event)s.%(date)s) DESC, %(event)s.%(date)s ASC
    """ % {
    'event' : connection.ops.quote_name(opts.db_table),
    'date'  :
connection.ops.quote_name(opts.get_field('date').column),
    'limit' : connection.ops.limit_offset_sql(5),
    }])
    return render_to_response('events/index.html', {'event_list':
event_list})


The above index view retrieves all the events for the 5 most recent
days and orders them how I'd like.

Essentially, my goal is to be able to provide previous and next links
such that older/newer events can be viewed as well. From an SQL
perspective, to obtain the data for each subsequent/previous page, I
would only need to apply an offset to the inner query to accomplish
this, but I don't know how to make that idea fit nicely in the world
of Django.

I thought maybe ObjectPaginator [1] was the way to go, but I'm not
sure if that's an appropriate route. It seems to deal with queries
which are more straight forward than mine, and the way it does
limiting and offsets only applies to the outer query as far as I can
tell.

I'd really appreciate any suggestion and/or ideas on this ;-)

Thanks,

Michael


[1] http://www.djangoproject.com/documentation/models/pagination/


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

Reply via email to