Note that there are some explanatory texts on larger screens.

plurals
  1. POAnnotate in Django with generic relations
    primarykey
    data
    text
    <p>I am using <a href="http://blog.damontimm.com/django-hitcount-app-count-hits-views/" rel="nofollow">django-hitcount</a> to tally hits to my database objects. I want to count the hits by object to determine which object has the most hits in a given time range. The app has two models of interest here:</p> <pre><code>class Hit(models.Model): created = models.DateTimeField(editable=False) ip = models.CharField(max_length=40, editable=False) session = models.CharField(max_length=40, editable=False) user_agent = models.CharField(max_length=255, editable=False) user = models.ForeignKey(User,null=True, editable=False) hitcount = models.ForeignKey(HitCount, editable=False) class HitCount(models.Model): hits = models.PositiveIntegerField(default=0) modified = models.DateTimeField(default=datetime.datetime.utcnow) content_type = models.ForeignKey(ContentType, verbose_name="content cype", related_name="content_type_set_for_%(class)s",) object_pk = models.TextField('object ID') content_object = generic.GenericForeignKey('content_type', 'object_pk') </code></pre> <p>"Hit" logs each hit with a timestamp while HitCount stores overall number of hits. To get the hits by object and within a time range, I need to do the following:</p> <p>Filter Hit by date created Count number of hits per content_object (within time range filtered above) order by count calculated above return content_object and count</p> <p>This could be very expensive, so I planned on calcing/caching once per day.</p> <p>As a first step, I wanted to count the number of hits per content_object irrespective of time range.</p> <pre><code>limited_hc = Hit.objects.all().values('hitcount__content_object').annotate(count = Count('hitcount__object_pk')) </code></pre> <p>I immediately run into an issue:</p> <p>Cannot resolve keyword 'hitcount__content_object' into field. Choices are: created, hitcount, id, ip, session, user, user_agent</p> <p>After some digging, I found that <a href="http://code.djangoproject.com/ticket/10461" rel="nofollow">annotation and generic relations do not work well together</a>. If I use object_pk instead of content_object, it works fine, but then I don't have the name of the object.</p> <p>So my question: <strong>What is an alternative to achieve the same result? How can group by object but also retain the name?</strong></p> <p>I do have the model (content_type) and the id (object_pk), so I could always pull these separately, but that seems inelegant. . . </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload