-2

So this is my query and it displays only file name but I want 'id' also.

filenames=Upload.objects.values('file').distinct().filter(created_by=request.user).all()

this will generate only unique files as dict. But I want 'id' of those files also and if use 'id' inside that 'values' it will not give unique file names. So this is my issue. I'm okay with other methods as well to solve this issue.

Sharath Nayak
  • 197
  • 3
  • 15

2 Answers2

0

This will work

from django.db.models import F

Upload.objects.filter(created_by=request.user).values('file').distinct().annotate(id=F('pk'))
Yugandhar Chaudhari
  • 3,831
  • 3
  • 24
  • 40
-1

I don't think we can achieve it using Distinct.

So I used loop like this.

filenames=Upload.objects.filter(created_by=request.user).all()
seen = set()
result = []
for f in filenames:
    if f.file not in seen:
        result.append(f)
        seen.add(f.file)
print result
Sharath Nayak
  • 197
  • 3
  • 15