Django multitask inheritance annotations

I have a basic LoggedEvent model and several subclasses, for example:

class LoggedEvent(models.Model):
    user = models.ForeignKey(User, blank=True, null=True)
    timestamp = models.DateTimeField(auto_now_add=True)

class AuthEvent(LoggedEvent):
    good = models.BooleanField()
    username = models.CharField(max_length=12)

class LDAPSearchEvent(LoggedEvent):
    type = models.CharField(max_length=12)
    query = models.CharField(max_length=24)

class PRISearchEvent(LoggedEvent):
    type = models.CharField(max_length=12)
    query = models.CharField(max_length=24)

Users generate these events as they perform appropriate actions. I am trying to create a report on the use of how many of each type of event each user triggered last month. I am struggling with Django ORM, and while I'm around, I have a problem. Here is the request code:

def usage(request):
    # Calculate date range
    today = datetime.date.today()
    month_start = datetime.date(year=today.year, month=today.month - 1, day=1)
    month_end = datetime.date(year=today.year, month=today.month, day=1) - datetime.timedelta(days=1)

    # Search for how many LDAP events were generated per user, last month
    baseusage = User.objects.filter(loggedevent__timestamp__gte=month_start, loggedevent__timestamp__lte=month_end)
    ldapusage = baseusage.exclude(loggedevent__ldapsearchevent__id__lt=1).annotate(count=Count('loggedevent__pk'))
    authusage = baseusage.exclude(loggedevent__authevent__id__lt=1).annotate(count=Count('loggedevent__pk'))

    return render_to_response('usage.html', {
        'ldapusage' : ldapusage,
        'authusage' : authusage,
    }, context_instance=RequestContext(request))

Both ldapusage and authusage are a list of users, each user is annotated with the .count attribute, which should represent how many specific events are generated by the user. However, in both lists, the .count attributes have the same meaning. Infact annotated "score" is equal to the number of events created by the user, regardless of type. So it would seem that my particular

authusage = baseusage.exclude(loggedevent__authevent__id__lt=1)

. id__lt = 1, id__isnull = True . Halp.

+5
1

Django , , , . , , , , , ( " . AuthEvent? . LDAP?..." ). , , .

: (ldap_event_count = LDAPEvent.objects.filter(user=foo).count(),...), . , , , :

content_type = models.ForeignKey("contenttypes.ContentType")

: , , - , (, event.authevent event.ldapevent) DoesNotExist. , - Event.objects.aggregate(Count("content_type")), , , ( " - Auth LDAP ..." ),

+4

All Articles