Remove duplicates in a Django query

前端 未结 6 1195
北恋
北恋 2020-12-08 06:39

Is there a simple way to remove duplicates in the following basic query:

email_list = Emails.objects.order_by(\'email\')

I tried using

6条回答
  •  孤街浪徒
    2020-12-08 06:57

    You may be able to use the distinct() function, depending on your model. If you only want to retrieve a single field form the model, you could do something like:

    email_list = Emails.objects.values_list('email').order_by('email').distinct()
    

    which should give you an ordered list of emails.

提交回复
热议问题