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 Hashmi <[email protected]> wrote: > 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.CharField(max_length=120) > description = models.TextField(blank=True, null=True) > price = models.DecimalField(decimal_places=2, max_digits=20) > active = models.BooleanField(default=True) > categories = models.ManyToManyField('Category', blank=True) > default = models.ForeignKey('Category', related_name='default_category', > null=True, blank=True) > hits = models.ForeignKey(HitCount) > > objects = ProductManager() > > class Meta: > ordering = ["-title", '-hits'] > > > The idea is to access Hitcount field hits to count max value and sort > list by max count. Below is Hitcount model installed as third party > reusable app djagno-hitcount. > > HitCount(models.Model): > hits = models.PositiveIntegerField(default=0) > modified = models.DateTimeField(auto_now=True) > content_type = models.ForeignKey(ContentType, > related_name="content_type_set_for_%(class)s", on_delete=models.CASCADE) > object_pk = models.PositiveIntegerField('object ID') > content_object = GenericForeignKey('content_type', 'object_pk') > > objects = HitCountManager() > > class Meta: > ordering = ('-hits',) > get_latest_by = "modified" > verbose_name = _("hit count") > verbose_name_plural = _("hit counts") > unique_together = ("content_type", "object_pk") > db_table = "hitcount_hit_count" > > > I need to call products in multiple apps with different list and in > my mainpage views I am trying to use filter option like below: > > from django.db.models import Max, Sum, Count > product1 = Product.objects.all().annotate(hits=Count('-hits__hits'))[:6] > > or > > product1 = Product.objects.all().order_by('-hits__hits')[:6] > > and with few different filter options. I couldn't make it work so advise > would be appreciated. > > -- > 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/39e610bf-0558-4a8d-8491-f9324d8986f1%40googlegroups.com > <https://groups.google.com/d/msgid/django-users/39e610bf-0558-4a8d-8491-f9324d8986f1%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/CAK52boXyqDJMFndLHALhrXnpV5%3Dj-mH8xiX45XJkw0F7HR9Srg%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.

