I have a page that needs to display basic user information plus the
last logged activity date for the user, and I need to be able to sort
by that value. (there are two of these relations,I'm just mentioning
one here)  My SQL-fu is very, very weak and I just can't seem to get
it.

There is a table in the middle that is kind of like a many to many,
but with a generic foreign key on one side, EventRelation.  That has
the content_type and object_id values for user, and a normal
foreignkey into the LoggedEvent model that has the field I need.

class LoggedEvent(models.Model):
     when = models.DateTimeField()
     summary = models.CharField(maxlength=255,null=True)
     ....

class EventRelation(models.Model):
    event = models.ForeignKey('LoggedEvent')
    objects=generic_relations.GenericManager()
    content_type = models.ForeignKey(ContentType,null=True,blank=True)
    object_id = models.PositiveIntegerField(null=True,blank=True)
    obj = models.GenericForeignKey()

I've tried using extra, but what I thought might work just grabs the
latest event by any user, not the latest event per user (idxlead
model).  If I could apply that select to each user, that is exactly
what I think I'm looking for:

extra['last_event_when'] = "SELECT event_loggedevent.when FROM
event_loggedevent,event_eventrelation WHERE
((event_eventrelation.object_id = common_idxlead.id) AND
(event_eventrelation.content_type_id = %s)) ORDER BY
event_loggedevent.when DESC limit 1" % ct

ct is the contenttype pk for the idxlead model.

There are quite a few users (25,000), and a lot more events.  I
thought about adding a field on the user model and updating that on
each event, but I was worried about an extra write on almost every
request.  I'd appreciate any suggestions.


--~--~---------~--~----~------------~-------~--~----~
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