[Django]-How to return most popular items in a table, but where each item is unique?

1👍

Does this work?

from collections import Counter 
results = Counter([vid for vid, eid in ActiveVenue.objects.values_list("venue_id", "event_id").distinct()]
👤monkut

0👍

See if something like this works

ActiveVenue.objects.all().annotate(score=Count('event__user', distinct=True)).order_by('-score')

0👍

This is my version (finish test):

models.py

class Venue(models.Model):
    name = models.CharField(max_length=200)

    def __unicode__(self):
        return self.name

    def ranking(self):
        count=0
        actives = ActiveVenue.objects.filter(
            venue__name=self.name).values(
            'event__user__username', 'venue__name').distinct()
        for active in actives:
            count += 1
        return count

views.py

def getRanking( anObject ):
    return anObject.ranking()

def myview(request):
    venues = list(Venue.objects.filter())
    venues.sort(key=getRanking, reverse=True)
    return render(request,'page.html',{'venues': venues})

templates

{% for venue in venues %}
    {{forloop.counter}}. {{venue}}<br/>
{% endfor %}

Output:

  1. The Oaks
  2. The Pound
  3. The Hill

0👍

Here is my take on the problem. First, here is a query that will grab the Venue ids and the scores corresponding to the related users.

testquery = ActiveVenue.objects.values("venue").annotate(score=Count("event__user", distinct=True)).order_by("-score")

Result

[{'score': 3, 'venue': 2}, {'score': 2, 'venue': 3}, {'score': 1, 'venue': 1}]

Next, the venue ids will be placed in a new list.

query_ids = [item["venue"] for item in testquery]
[2, 3, 1]

Next, the corresponding Venue objects have to be obtained.

tempresult = Venue.objects.filter(id__in=query_ids)
[<Venue: The Hill>, <Venue: The Oaks>, <Venue: The Pound>

Finally, a list comprehension will be performed to re-order the Venue objects based on the scores obtained earlier.

result = [venue for venue_id in query_ids for venue in tempresult if venue_id == venue.id]
[<Venue: The Oaks>, <Venue: The Pound>, <Venue: The Hill>]

This gives the proper result based on the test data.

Leave a comment