Limit 1 after filter in django

Question:

SystemsV2.objects.filter(Q(sysname = 'DC_BW_SBX_S4H') | Q(sysname='DC_BW_DEV_S4H')).values('sysname')

Executing the above command gives this output,

<QuerySet [{'sysname': 'DC_BW_SBX_S4H'}, {'sysname': 'DC_BW_SBX_S4H'}, {'sysname': 'DC_BW_DEV_S4H'}]>

How can we make it so that only one of each exists in the result. For example,

<QuerySet [{'sysname': 'DC_BW_SBX_S4H'}, {'sysname': 'DC_BW_DEV_S4H'}]>
Asked By: Abhinav

||

Answers:

Use .distinct()

SystemsV2.objects.filter(Q(sysname = 'DC_BW_SBX_S4H') | Q(sysname='DC_BW_DEV_S4H')).values('sysname')
    .order_by('sysname').distinct()
Answered By: Ezon Zhao
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.