Todor,

You have access to my complete repo but still I will post answer here for
others to learn.

Regards,
Mudassar

On Fri, Aug 5, 2016 at 6:06 AM, Todor Velichkov <[email protected]>
wrote:

> Honestly, now Idea why you get this error, can you paste some more code?
> And maybe some stack trace?
>
> On Friday, August 5, 2016 at 2:20:25 PM UTC+3, M Hashmi wrote:
>>
>> 1.8.13 as its LTS.
>>
>> On Fri, Aug 5, 2016 at 3:54 AM, Todor Velichkov <[email protected]>
>> wrote:
>>
>>> Whats your Django version?
>>>
>>> On Friday, August 5, 2016 at 1:42:48 PM UTC+3, M Hashmi wrote:
>>>>
>>>> I did the same thing while working with the contenttype except I did
>>>> used default filters .all(), .filter(), .get(), annotations and
>>>> aggregations etc. Todor each product shows multiple hits if I log out and
>>>> hit it and login back to hit it shows more than one hits. Hits are being
>>>> recoreded for each session. It shows the same error as it is showing now
>>>> after implementing GenericRelations:
>>>>
>>>> AttributeError at / 'GenericForeignKey' object has not attribute
>>>> 'get_lookup'.
>>>>
>>>> If I mix it with .aggregate(Max, Count, Avg or Sum) queryset options it
>>>> will pop up "unhashable type".
>>>>
>>>> I am working on joins methods in django with hope that it will make it
>>>> work. I really appreciate your help guys. You both rock and I've learned a
>>>> lot with your directions. Still if you have ideas please let me know I will
>>>> try those too.
>>>>
>>>> Regards,
>>>> Mudassar
>>>>
>>>> On Fri, Aug 5, 2016 at 1:04 AM, Todor Velichkov <[email protected]>
>>>> wrote:
>>>>
>>>>> 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('hitc
>>>>> ounts__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]> wrote:
>>>>>>
>>>>>>> Hello Todor,
>>>>>>>
>>>>>>> I followed your directions and used https://docs.djangoprojec
>>>>>>> t.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].
>>>>>>> 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/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
>>>>> <https://groups.google.com/d/msgid/django-users/cdc4459f-0f03-4fcd-876e-814b25b241c0%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/ms
>>> gid/django-users/0e1140b5-5971-43d3-8a9f-0e81c3fa8a4c%40googlegroups.com
>>> <https://groups.google.com/d/msgid/django-users/0e1140b5-5971-43d3-8a9f-0e81c3fa8a4c%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/cf6710ef-cde9-48f0-b50f-b06e49cbbc2b%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/cf6710ef-cde9-48f0-b50f-b06e49cbbc2b%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/CANoUts6q__ckTdDv_6JVcL72-NTsG3c4TAN8ABzpgxbX%2BCCCRw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to