Constantine Covtushenko, 

HitCount is a model from a 3rd party app, so the OP can't change it. 

However the way HitCount is implemented I don't think one there is a way 
for one product to have more than one `HitCount` objects. 
So I don't think any aggregations are needed. 

Lets the Product class look like this:

class Product(models.Model):
    #im changing this on purpose, because it points to HitCount class.
    hitcounts = GenericRelation(HitCount, content_type_field=
'content_object', object_id_field='object_pk',)

Now `HitCount` has a separate `Hit` class  which hold all individual hits 
for a `product`. 

The app model structure is here. 
<https://github.com/thornomad/django-hitcount/blob/master/hitcount/models.py>

So to get top products by overall hits you can use `HitCount.hits` field, 
without any `aggregations`, `annotations` or whatsoever.

top_products_by_total_hits = Product.objects.order_by('hitcounts__hits')[:6]

Now if you want to get the number of hits for a product for some period of 
range (lets say a week). Then we need to use the `Hit` model.

top_products_by_last_week_hits = Product.objects.filter(
        hitcounts__hit__created__gte=today-timedelta(days=7)
    ).annotate(
        last_week_hits=Count('hitcounts__hit')
    ).order_by('-last_week_hits')[:6]

Hope this helps and things get clear now.

On Friday, August 5, 2016 at 8:44:02 AM UTC+3, Constantine Covtushenko 
wrote:
>
> Hi M Hashmi,
>
> I believe that you are looking a way to use aggregation on related model.
> You should try the following snitppet:
> `trending_products = Product.objects.aggregate(hit=Max('hits__hits'))[:6]` 
> (see more info here 
> <https://docs.djangoproject.com/en/1.9/topics/db/aggregation/>)
>
> This will give you list of products where each product will have 'hit' is 
> the MAX value of all hits set for this product.
>
> Also I do not see any reason to use 'ContentType' relation from your 
> HitCount model at all.
> If your origin intent was to have hits with any Model in your 
> system(project) then you should built 'joins with aggregation' queries 
> based on Reverse Generic Relation as said Todor. But in this case Product 
> hits field becomes unnecessary as it duplicates relations.
>
> On Fri, Aug 5, 2016 at 5:59 AM, M Hashmi <[email protected] <javascript:>
> > wrote:
>
>> Hello Todor,
>>
>> I followed your directions and used 
>> https://docs.djangoproject.com/ja/1.9/ref/contrib/contenttypes/ for 
>> reference but I got stuck at error 'GenericForeignKey' object has not 
>> attribute 'get_lookup'. I tried 
>> Product.objects.aggregate(Count('hits'))[:6]. In my models.py I got 
>> following code:
>>
>> hits = GenericRelation(HitCount, content_type_field='content_object', 
>> object_id_field='object_pk',)
>>
>>     class Meta:
>>         ordering = ["-title"]
>> Removed the -hits from ordering section and rest of the code is same. I 
>> searched it on google but it showed some Django based bugs.
>> Any suggestions?
>> Thanks,
>>
>>
>>
>> On Thursday, August 4, 2016 at 2:07:53 PM UTC-7, Todor Velichkov wrote:
>>>
>>> My field hits=models.ForeignKey(Hitcount) means that my Product model 
>>>> has a related  model with multiple fields and all the products in Product 
>>>> model will have one or more hit records. Instance of Product model will 
>>>> save a hit from end user by session/ip/user etc.
>>>
>>> Honestly, I don't understand that. 
>>>
>>> The HitCount class already has a GenericForeignKey, maybe you are 
>>> looking for a Reverse Generic Relation 
>>> <https://docs.djangoproject.com/ja/1.9/ref/contrib/contenttypes/#reverse-generic-relations>
>>>  
>>> class in order to get the Hits for a product.
>>>
>>> On Thursday, August 4, 2016 at 10:42:58 PM UTC+3, M Hashmi wrote:
>>>>
>>>> My field hits=models.ForeignKey(Hitcount) means that my Product model 
>>>> has a related  model with multiple fields and all the products in Product 
>>>> model will have one or more hit records. Instance of Product model will 
>>>> save a hit from end user by session/ip/user etc.
>>>>
>>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected] 
>> <javascript:>.
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/2a7be23a-c227-460d-89f2-d7ffd52d4123%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/django-users/2a7be23a-c227-460d-89f2-d7ffd52d4123%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/cdc4459f-0f03-4fcd-876e-814b25b241c0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to