OpenTechStrategies / streetcrm

StreetCRM is a free software contact management application
Other
5 stars 4 forks source link

Events not sortable by attendee count #98

Open nttaylor opened 9 years ago

nttaylor commented 9 years ago

On "Select Event to Change" page, the table is not sortable by number of attendees or date of action. This seems related to the fact that these columns are generated by method calls on the event object while other columns are not.

cecilia-donnelly commented 9 years ago

I think this is relatively low priority, since it is only relevant to Attendee Count, but here is a response that will probably help:

http://stackoverflow.com/questions/2168475/django-admin-how-to-sort-by-one-of-the-custom-list-display-fields-that-has-no-d

Then again, this might be closer to what we need, since the Attendee Count is a calculated field: https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.extra (was sent to that link by this answer: http://stackoverflow.com/questions/1652577/django-ordering-queryset-by-a-calculated-field)

cecilia-donnelly commented 9 years ago

I tried adding this to admin.py:

+    def queryset(self, request):
+        qs = super(CustomerAdmin, self).queryset(request)
+        qs = qs.annotate(models.Count('order'))
+        return qs
+
+    def attendee_count(self, obj):
+        return int(obj.participants.all().count())
+
+    attendee_count.admin_order_field = attendee_count
+

But I got this error:

AttributeError at /swoptact/event/

'function' object has no attribute 'startswith'

That error seems to imply that (1) django is seeking to order the attendee count as a string and (2) that it isn't ordering by the result of the function, but rather the function itself (maybe).

Not sure where to go from here!