1👍
Does this work?
from collections import Counter
results = Counter([vid for vid, eid in ActiveVenue.objects.values_list("venue_id", "event_id").distinct()]
0👍
See if something like this works
ActiveVenue.objects.all().annotate(score=Count('event__user', distinct=True)).order_by('-score')
- [Django]-Remove a migration from south migration history
- [Django]-Django localization: labels don't get updated
- [Django]-How to remove get parameter from url in django view
- [Django]-Use context processor only for specific application
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:
- The Oaks
- The Pound
- The Hill
- [Django]-Django over https form redirection issues
- [Django]-Allowing basic html markup in django
- [Django]-Django user in tornado
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.
- [Django]-Django ORM – Grouped aggregates with different select clauses
- [Django]-Resize image in the views and load it in the templates in django
- [Django]-Implement get_serializer in generic list view (generics.ListCreateAPIView) in Django REST Framework
- [Django]-Django localization: labels don't get updated
- [Django]-Remove a migration from south migration history
Source:stackexchange.com