0

Does anyone know how to get a distinct value in Django QuerySet? I'm using MySQL db and I couldn't find a solution yet.

The table is:

         ID     WORK_YMD    LINE_NM  MODEL_CODE    MAG_NO             PRODC_MAGT_NO 
        118002  20191015    PBA-21F BN94-14806W A656MAF00001    BR10BN9414806WA656MAE4035
        118003  20191015    PBA-21F BN94-14806W A656MAF00001    BR10BN9414806WA656MAE4027
        118004  20191015    PBA-21F BN94-14806W A656MAF00001    BR10BN9414806WA656MAE4039
        118005  20191015    PBA-21F BN94-14806W A656MAF00001    BR10BN9414806WA656MAE4037
        118006  20191015    PBA-21F BN94-14806W A656MAF00001    BR10BN9414806WA656MAE4038

I want to get the model_code considering a Filter mag_no = "My Variable" Ex:A656MAF00001

I tried something like this, but didn't work.

Magazine.objects.filter(mag_no='A656MAF00001').get('model_code').distinct().order_by()

The error is:

"Too many values to unpack"

4
  • you have several PRODC_MAGT_NO for each A656MAF00001 MAG_NO so the distinct clause return all the rows with different value .. are you sure you are looking for distinct and not for min or max PRODC_MAGT_NO related to MAG_NO Commented Oct 16, 2019 at 12:58
  • Try Magazine.objects.filter(mag_no='A656MAF00001').order_by('model_code).distinct('model_code') Commented Oct 16, 2019 at 13:02
  • Tks Lancross, but I got the follow error: DISTINCT ON fields is not supported by database backend Commented Oct 16, 2019 at 13:09
  • Hello scaisEdge, my simple aplication is to the user pass the mag_no and django gives model_code, after that a I gonna include other values to save in another table, it's just to validate Commented Oct 16, 2019 at 13:13

2 Answers 2

2

try:

Magazine.objects.filter(mag_no='A656MAF00001').values('model_code').distinct().order_by('-id')

You can't give arguments to distinct unless you are using Postgres.

From the docs:

On PostgreSQL only, you can pass positional arguments (*fields) in order to specify the names of fields to which the DISTINCT should apply.

Django docs distinct

edit: based on the comment to get the last row.

Magazine.objects.filter(mag_no='A656MAF00001').values('model_code').distinct().last().get('model_code')
Sign up to request clarification or add additional context in comments.

3 Comments

Really thanks, this definetelly works. But gave a list, possible to get only the last value? I Tried Magazine.objects.filter(mag_no='A656MAF00001').values('model_code').distinct().order_by('-id').last() , but I have {'model_code': 'BN94-14806W'}, how to get only BN94-14806W
Exactly that, Thanks a lot!
Glad it helped! Please, consider accepting my answer.
0

As an improvement on Nalin Dobhal's answer, and in response to your comment to his/her answer, do this:

Magazine.objects.filter(mag_no='A656MAF00001').values('model_code').distinct().order_by('-id').last().get('model_code')

This is because, after invoking last(), you are left with a python dict, and to get a dict's value, you need to use the get('key_name') function. As pointed out here:https://realpython.com/python-dicts/#dgetltkeygt-ltdefaultgt

1 Comment

Thank you Mr. Abouchama, now I got 100% that I want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.