<gh_stars>0
from django.contrib import admin
from django.utils.timesince import timesince
from django.utils.translation import ugettext_lazy as _
from django.db.models.aggregates import Count, Sum
from django.db.models.functions import Coalesce
from .models import User
from allauth_extras.admin import BaseUserAdmin, BaseUserChangeForm


class UserChangeForm(BaseUserChangeForm):
    pass


@admin.register(User)
class UserAdmin(BaseUserAdmin):
    add_form = form = UserChangeForm
    readonly_fields = 'last_login',
    list_display = 'username', 'email', 'first_name', 'last_name', 'trv', 'joined',
    list_filter = 'date_joined', 'is_staff', 'is_superuser', 'is_active', 'groups'
    search_fields = ('username', 'first_name', 'last_name', 'email')

    def get_queryset(self, request):
        return (
            super().get_queryset(request)
            #.annotate(annotations_count=Count('annotations', distinct=True))
            .annotate(trv=Coalesce(Sum('wallet__entries__amount'), 0))
        )

    def annotations(self, obj):
        return obj.annotations_count
    annotations.short_description = _('Annotations')
    annotations.admin_order_field = 'annotations_count'

    def trv(self, obj):
        return obj.trv
    trv.short_description = _('TRV')
    trv.admin_order_field = 'trv'

    def joined(self, obj):
        return timesince(obj.date_joined) + ' ago'
    joined.short_description = _('Joined')
    joined.admin_order_field = 'date_joined'

from django.contrib import admin
from django.utils.timesince import timesince
from django.utils.translation import ugettext_lazy as _
from django.db.models.aggregates import Count, Sum
from django.db.models.functions import Coalesce
from .models import User
from allauth_extras.admin import BaseUserAdmin, BaseUserChangeForm


class UserChangeForm(BaseUserChangeForm):
    pass


@admin.register(User)
class UserAdmin(BaseUserAdmin):
    add_form = form = UserChangeForm
    readonly_fields = 'last_login',
    list_display = 'username', 'email', 'first_name', 'last_name', 'trv', 'joined',
    list_filter = 'date_joined', 'is_staff', 'is_superuser', 'is_active', 'groups'
    search_fields = ('username', 'first_name', 'last_name', 'email')

    def get_queryset(self, request):
        return (
            super().get_queryset(request)
            #.annotate(annotations_count=Count('annotations', distinct=True))
            .annotate(trv=Coalesce(Sum('wallet__entries__amount'), 0))
        )

    def annotations(self, obj):
        return obj.annotations_count
    annotations.short_description = _('Annotations')
    annotations.admin_order_field = 'annotations_count'

    def trv(self, obj):
        return obj.trv
    trv.short_description = _('TRV')
    trv.admin_order_field = 'trv'

    def joined(self, obj):
        return timesince(obj.date_joined) + ' ago'
    joined.short_description = _('Joined')
    joined.admin_order_field = 'date_joined'
#.annotate(annotations_count=Count('annotations', distinct=True))