0

Given a Django Admin module for a model that has fields for an ID and datetime, is it possible to show only the latest entity for each group defined by ID?

Would it be possible to do with a filter and have it applied by default?

IDSK
  • 1

1 Answers1

0

Yep, you could override get_queryset() (docs) and filter however you like. Eg:

class MyModelAdmin(admin.ModelAdmin):
    def get_queryset(self, request):
        qs = super().get_queryset(request)
        return qs.order_by('user_id', '-date').distinct('user_id')

Note distinct() will only work with positional arguments like this if you're using PostgreSQL (see docs)

See also these answers:

Override get_queryset

Filter latest by field

Kit
  • 53
  • 7