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 django-users...@googlegroups.com.
To post to this group, send email to django...@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/39e610bf-0558-4a8d-8491-f9324d8986f1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
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.
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.
To post to this group, send email to django...@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/81557c06-29ac-44d0-b76c-b3fea37c83f2%40googlegroups.com.
--
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.
To post to this group, send email to django...@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/2a7be23a-c227-460d-89f2-d7ffd52d4123%40googlegroups.com.
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',)
top_products_by_total_hits = Product.objects.order_by('hitcounts__hits')[:6]
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]
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@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/cdc4459f-0f03-4fcd-876e-814b25b241c0%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@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/0e1140b5-5971-43d3-8a9f-0e81c3fa8a4c%40googlegroups.com.
1.8.13 as its LTS.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@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/cf6710ef-cde9-48f0-b50f-b06e49cbbc2b%40googlegroups.com.