django-aggregation

django queryset aggregation count counting wrong thing

大兔子大兔子 提交于 2019-12-12 18:31:25
问题 This is a continuation question from: Django queryset get distinct column values with respect to other column My Problem: Using aggregate count in Django counts the wrong thing in the queryset, or as far as I can see something that is not even in my queryset. What I did I used: queryset.order_by('col1', 'col2').distinct('col1', 'col2').values('col2') to get the values of col2 of a model where all the rows have a distinct pair of values in (col1, col2) . There is an example in the link above.

Exclude some Posts from count of tags

陌路散爱 提交于 2019-12-11 15:54:10
问题 I'm using django-taggit to manage my tag. I want to include a list of used tags with the indication of how many times each of them has been used. To do so I'm using taggit_templatetags2 but I can avoid. My models.py : from taggit.managers import TaggableManager class Post(models.Model): ... tags = TaggableManager(blank=True) My template.html : {% load taggit_templatetags2_tags %} {% get_taglist as tags for 'blog.post' %} {% for tag in tags %} {% if tag.slug != 'draft' and tag.slug != 'retired

How to use annotate in query set to extract a count of type in a location?

◇◆丶佛笑我妖孽 提交于 2019-12-11 08:05:38
问题 I can't seem to properly use annotate to extract the information I need from my models. I have the follow .model structure: class ArtistWorkPlaceQuerySet(models.QuerySet): def with_related(self): return self.select_related('artist','work', 'place') class ArtistWorkPlaceManager(models.Manager): pass class PersonWorkPlace(models.Model): artist = models.ForeignKey(Artist, verbose_name=_('artist'), related_name='work', on_delete=models.CASCADE) work = models.ForeignKey(Work, verbose_name=_('work'

Django - aggregation with get_FOO_display

◇◆丶佛笑我妖孽 提交于 2019-12-11 03:14:54
问题 Consider the following: status = queryset.values('status').annotate(count=Count('status')) where status field is a CharField with choices . This will result in a list of dictionaries with status DB value along with its count. Is there a way to aggregate status and show its display value instead? I have looked up the code of _get_FIELD_display which I probably can emulate, but it feels a tad hackish to repeat the framework's internal code. 回答1: Without hacking a SQL, you may not achieve this

Annotating SUM aggregation function leading to 'None' value in Django

主宰稳场 提交于 2019-12-10 12:55:12
问题 Doing my first real Django project, and need guidance. Background: My project is a reddit clone. Users submit links+text. Visitors upvote or downvote. There's a social_ranking algo, runs every ~2 mins as a background script, reranks all the submissions according to net votes and freshness of content. Fairly vanilla stuff. Problem: Ordering by votes isn't working correctly, because votes are being initialized as None instead of 0 . This causes submissions with None votes to rank below

Django conditional annotation

一世执手 提交于 2019-12-09 16:24:02
问题 I'm surprised that this question apparently doesn't yet exist. If it does, please help me find it. I want to use annotate (Count) and order_by, but I don't want to count every instance of a related object, only those that meet a certain criteron. To wit, that I might list swallows by the number of green coconuts they have carried: swallow.objects.annotate(num_coconuts=Count('coconuts_carried__husk__color = "green"').order_by('num_coconuts') 回答1: This should be the right way. swallow.objects

django: time range based aggregate query

試著忘記壹切 提交于 2019-12-04 14:28:56
问题 I have the following models, Art and ArtScore: class Art(models.Model): title = models.CharField() class ArtScore(models.Model): art = models.ForeignKey(Art) date = models.DateField(auto_now_add = True) amount = models.IntegerField() Certain user actions results in an ArtScore entry, for instance whenever you click 'I like this art', I save a certain amount of ArtScore for that Art. Now I'm trying to show a page for 'most popular this week', so I need a query aggregating only ArtScore amounts

Django conditional annotation

故事扮演 提交于 2019-12-04 03:17:35
I'm surprised that this question apparently doesn't yet exist. If it does, please help me find it. I want to use annotate (Count) and order_by, but I don't want to count every instance of a related object, only those that meet a certain criteron. To wit, that I might list swallows by the number of green coconuts they have carried: swallow.objects.annotate(num_coconuts=Count('coconuts_carried__husk__color = "green"').order_by('num_coconuts') This should be the right way. swallow.objects.filter( coconuts_carried__husk__color="green" ).annotate( num_coconuts=Count('coconuts_carried') ).order_by(

Grouping Django model entries by day using its datetime field

自作多情 提交于 2019-12-03 17:49:34
问题 I'm working with an Article like model that has a DateTimeField(auto_now_add=True) to capture the publication date (pub_date). This looks something like the following: class Article(models.Model): text = models.TextField() pub_date = models.DateTimeField(auto_now_add=True) I want to do a query that counts how many article posts or entries have been added per day. In other words, I want to query the entries and group them by day (and eventually month, hour, second, etc.). This would look

Get daily counts of objects from Django

做~自己de王妃 提交于 2019-12-03 09:32:51
问题 I have a Django model with a created timestamp and I'd like to get the counts of objects created on each day. I was hoping to use the aggregation functionality in Django but I can't figure out how to solve my problem with it. Assuming that doesn't work I can always fall back to just getting all of the dates with values_list but I'd prefer to give the work to Django or the DB. How would you do it? 回答1: Alex pointed to the right answer in the comment: Count number of records by date in Django