-1

I have a table in MySQL, like:

id  dirId filename
 1   1      jone
 2   1      jack
 3   2      jack
 4   3      ella

I want to get the biggest dir_id(and filename) with the same filename. We can use group by in MySQL, what to use in django?

coder
  • 8,346
  • 16
  • 39
  • 53
Joe
  • 1
  • 1

2 Answers2

0

I used those code to get it:
1.A1=ModelClass.objects.all() 2.A2=A1.values_list('filename',flat='True').distinct() 3.for itr in A2: A1.filter(filename=itr).order_by('dirId') don't know it's fast enough.

Joe
  • 1
  • 1
-1

you can use group by syntax in django like this.

ModelClass.objects.filter(...).values('filename').annotate(dir_id=models.Max('dirId'))
vorujack
  • 1,778
  • 16
  • 22