[Answered ]-How to make annotation for foreign key in Django?

1👍

The opposite way of the inviter relation is the customuser, so you can implement this as:

from django.db.models import Count

class CustomUserAdmin(admin.ModelAdmin):
    list_display = ['created', 'updated']

    def get_queryset(self, request):
        return super().get_queryset(request).annotate(
            points=Count('customuser')
        ).order_by('-points')

It might however make more sense to rename the related_name=… [Django-doc] of your inviter to invitees:

class CustomUser(models.Model):
    inviter = models.ForeignKey(
        'self',
        related_name='invitees',
        on_delete=models.SET_NULL,
        null=True,
        blank=True
    )

    @property
    def _points(self):
        return self.invitees.count()

then we thus count this as:

class CustomUserAdmin(admin.ModelAdmin):
    list_display = ['created', 'updated']

    def get_queryset(self, request):
        return super().get_queryset(request).annotate(
            points=Count('invitees')
        ).order_by('-points')

Leave a comment