Re: QuerySet for related models

2016-08-05 Thread M Hashmi
Thanks Todor/Constantine for your help, I've added a field in my Product models with generic relations like: hitcounts = GenericRelation(HitCount, content_type_field='content_type', object_id_field='object_pk',) In my view: object_list = Product.objects.order_by('-hitcounts__hits')[:6] In my tem

Re: QuerySet for related models

2016-08-05 Thread M Hashmi
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 wrote: > Honestly, now Idea why you get this error, can you paste some more code? > And maybe some stack trace? > > On Friday, Aug

Re: QuerySet for related models

2016-08-05 Thread Todor Velichkov
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 > wrote: > >> Whats your Django version? >> >> On Friday,

Re: QuerySet for related models

2016-08-05 Thread M Hashmi
1.8.13 as its LTS. On Fri, Aug 5, 2016 at 3:54 AM, Todor Velichkov 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(),

Re: QuerySet for related models

2016-08-05 Thread Todor Velichkov
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

Re: QuerySet for related models

2016-08-05 Thread M Hashmi
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

Re: QuerySet for related models

2016-08-05 Thread Todor Velichkov
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 cla

Re: QuerySet for related models

2016-08-04 Thread M Hashmi
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.

Re: QuerySet for related models

2016-08-04 Thread Constantine Covtushenko
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 ) This will gi

Re: QuerySet for related models

2016-08-04 Thread M Hashmi
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 follow

Re: QuerySet for related models

2016-08-04 Thread M Hashmi
Thank you Constantine, I got this mistake fixed and this was really bad mistake from my side. Now with your proposed scenario I am trying to get it work with "trending_products = Product.objects.all().order_by('hits__hits')[:6]" but still it isn't giving me sort by max hits as I used annotate m

Re: QuerySet for related models

2016-08-04 Thread M Hashmi
Thanks Todor as this make senseall I need is to get the Hitcount's hits presented as local variable to Product model so in queryset I can set order_by. I will try this and will post results. On Thursday, August 4, 2016 at 2:07:53 PM UTC-7, Todor Velichkov wrote: > > My field hits=models.Fore

Re: QuerySet for related models

2016-08-04 Thread Constantine Covtushenko
Please check my notes below: On Thu, Aug 4, 2016 at 10:42 PM, 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. > Sorry but I do not see ho

Re: QuerySet for related models

2016-08-04 Thread Todor Velichkov
> > 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 u

Re: QuerySet for related models

2016-08-04 Thread M Hashmi
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

Re: QuerySet for related models

2016-08-04 Thread Constantine Covtushenko
Hi M Hashmi, As I see your model scheme built with meaning that 1 record of HitCount relates to many records of Product. And in your queries you try to select all products that relate to 5 topmost HitCounts. Am I correct? Will be waiting for your response. On Thu, Aug 4, 2016 at 6:41 PM, M Has

QuerySet for related models

2016-08-04 Thread M Hashmi
I know that count only can be applied for an IntegerField or FloatField but I've created a ForeignKey to another model which contains hitcounts. Now I need to filter results by max count from related model and I couldn't dot it. models.py: class Product(models.Model): title = models.Ch