Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It will probably be more efficient for your purpose to add a generic relation to <code>Hit</code> model:</p> <pre><code>class Hit(models.Model): ... object_id = models.PositiveIntegerField() content_type = models.ForeignKey(ContentType) content_object = generic.GenericForeignKey('content_type', 'object_id') </code></pre> <p>and then run count() query on the Hit directly:</p> <pre><code>t = ContentType.objects.get_for_model(the_object_being_hit) id = the_object_being_hit.id count = Hit.objects.filter( created__range=(from_timestamp, to_timestamp), content_type = t, object_id = id ).count() </code></pre> <p>You can use Django South migration system to modify the hit-count's model. You can also try to subclass Hit after monkey-patching it's Meta class, or just define your own models that suit your needs better.</p> <p><strong>edit</strong> If you want to count hits for a whole class of objects or several classes, then you can have:</p> <pre><code>count = Hit.objects.filter( created__range = myrange, content_type__in = set_of_types ).count() </code></pre> <p>Where the <code>set_of_types</code> can be either a list constructed with <code>get_for_model</code> calls or a query set obtained by direct filtering of the <code>ContentType</code> table.</p> <p>The nice part of the count() method is that it makes counting happen in the database, which is much faster.</p> <p>To get a breakdown by content_type try this:</p> <pre><code>counts = Hit.objects.filter( created__range = myrange ).values( 'content_type' ).annotate( Count('content_type') ) </code></pre> <p>That should return a dictionary of counts vs content type id, quite close to what you want.</p>
    singulars
    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.
 

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