3

I have model called StudentAppeared

class StudentAppeared(models.Model):
    roll_number = models.CharField(max_length=50)

Assume i have following data

enter image description here

From the above data result should display

enter image description here

How can i achieve this thing using django query for StudentAppeared model?

2 Answers 2

5
StudentAppeared.objects.distinct()

sorry made a mistake. fixed it

Sign up to request clarification or add additional context in comments.

2 Comments

This returns all five rows as the ids are distinct.
Also take into consideration too that this doesn't work on SQLite backends (if you are in development)
2

This returns unique roll_numbers with the highest associated id, sorted by id:

from django.db.models import Max

stats = (
    StudentAppeared.objects
    .values('roll_number')
    .annotate(max_id=Max('id'))
    .values_list('max_id', 'roll_number ')
    .order_by('max_id')
)

Comments

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.