Thanks for reply and I tried this before same exact code but I got stuck at 
"unhashable type" error.
I searched it thoroughly but then I gave up on it to have some guidance 
from kind people like you.

If you believe this is how it should work then please provide me some quick 
ref for the error. I also changed it like:
trending_products = list(Product.objects.aggregate(hit=Max('hits__hits')))
It didn't work.

Thanks again.



On Thursday, August 4, 2016 at 10:44:02 PM UTC-7, 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 <mhash...@gmail.com <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 django-users...@googlegroups.com <javascript:>.
>> To post to this group, send email to django...@googlegroups.com 
>> <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 django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
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/8896d210-1e8f-40d5-8580-cc5b15db51d3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to