1

I have model having fields as name, school, email, address etc.

I want to find out the rows for which there is same entry for name and school field ?

eg:
name school email 
abc     mps    abc@gmail.com
abc     mps    abc@gmail.com
xyz     vps    xyz@gmail.com
abc     mps    abc@gmail.com
poi     vps    poi@gmail.com
jkl     vps    jkl@gmail.com

like for name abc and school mps ther are 3 entries and for xyz and vps there are 2 entries I can nested for loop and iterative manner to check the name and school fields against all the rows in the table. That would be performance hit (n*n)

Is there any other way to find out ?

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
navyad
  • 3,752
  • 7
  • 47
  • 88
  • do you have the name and school you are looking for or do you want to get all counts name/school combinations? – dm03514 May 28 '14 at 16:20
  • I think it's solved here http://stackoverflow.com/questions/8989221/django-select-only-rows-with-duplicate-field-values Basically you group by name or school and then check if count > 1 – FrEaKmAn May 28 '14 at 16:21

2 Answers2

0

distinct() will serve the purpose. The following will solve your problem

model_name.objects.values_list('name').distinct()

If you want dictionary then you have to do this

model_name.objects.values('name').distinct()

Hope that solves you issue!!

S.Ali
  • 664
  • 1
  • 5
  • 12
  • @FrEaKmAn "Returns a new QuerySet that uses SELECT DISTINCT in its SQL query. This eliminates duplicate rows from the query results." thats what the official docs say when you goto the link that i have mentioned – S.Ali May 28 '14 at 16:26
  • 1
    but how does this solve the question I want to find out the rows for which there is same entry for name and school field ? – FrEaKmAn May 29 '14 at 12:59
0

I'm not sure if I understand you, you just want to get the results wich has the SAME name and school, or just want to get ONE result for name and school ?

This sentence will return ALL schools with name 'abc' and school 'mps'

result_list = model.objects.filter(name='abc').filter(school='mps')


If you're trying to get only ONE result per name and school, you have to use distinct() like:

result_list = model.objects.filter(name='abc').filter(school='mps').distinct() `


AlvaroAV
  • 10,335
  • 12
  • 60
  • 91