Vor
Vor

Reputation: 35099

can't figure out how to use distinct() django

Help me please remove duplicates from my output:

views.py

if request.is_ajax():
        subjects = Subjects.objects.distinct().order_by('-num_of_followers')[:5]
        result = serializers.serialize("json", subjects) 

As you can see distinct() doesn't do anything.

My output right now looks like this:

aaa
aaa
aaa
bbb
ccc

but I want it to be like this:

aaa
bbb
ccc

UPDATED

right now my database looks like this:

Database changed
mysql> SELECT * FROM school_subjects;
+----+----------+---------+------------+---------------------+------------------+
| id | name     | user_id | created_by | created_time        | num_of_followers |
+----+----------+---------+------------+---------------------+------------------+
|  1 | Math 140 |       1 | rrr        | 2012-08-23 12:11:55 |                4 |
|  2 | lll      |       2 | aaa        | 2012-08-23 14:25:13 |                2 |
|  3 | kmkk     |       2 | aaa        | 2012-08-25 14:11:42 |                2 |
|  4 | llll     |       2 | aaa        | 2012-08-25 14:11:57 |                2 |
|  5 | Math 140 |       3 | qqq        | 2012-08-25 15:29:44 |                4 |
|  6 | qweqw    |       3 | qqq        | 2012-08-25 15:30:32 |                1 |
|  7 | lalala   |       3 | qqq        | 2012-08-25 15:38:57 |                1 |
|  8 | kkdkdk   |       3 | qqq        | 2012-08-25 17:49:25 |                1 |
|  9 | aaaa     |       2 | aaa        | 2012-08-27 19:13:49 |                1 |
| 10 | mmcmcm   |       2 | aaa        | 2012-08-27 19:22:10 |                1 |
| 11 | aaaaa    |       2 | aaa        | 2012-08-27 21:17:32 |                1 |
| 12 | Math 140 |       2 | aaa        | 2012-08-27 21:25:07 |                4 |
+----+----------+---------+------------+---------------------+------------------+
12 rows in set (0.00 sec)

JSON that I'm getting:

[{"pk": 1, "model": "school.subjects", "fields": {"created_time": "2012-08-23 12:11:55", "num_of_followers": 4, "name": "Math 140", "created_by": "rrr", "user": 1}}, {"pk": 12, "model": "school.subjects", "fields": {"created_time": "2012-08-27 21:25:07", "num_of_followers": 4, "name": "Math 140", "created_by": "aaa", "user": 2}}, {"pk": 5, "model": "school.subjects", "fields": {"created_time": "2012-08-25 15:29:44", "num_of_followers": 4, "name": "Math 140", "created_by": "qqq", "user": 3}}, {"pk": 4, "model": "school.subjects", "fields": {"created_time": "2012-08-25 14:11:57", "num_of_followers": 2, "name": "llll", "created_by": "aaa", "user": 2}}, {"pk": 3, "model": "school.subjects", "fields": {"created_time": "2012-08-25 14:11:42", "num_of_followers": 2, "name": "kmkk", "created_by": "aaa", "user": 2}}]

from this example you can see that I'm getting Math 140 3 times, but I want it to appear only once.

thank you.

Upvotes: 2

Views: 109

Answers (1)

jdi
jdi

Reputation: 92559

The docs have some specific information about using distinct and how its affected by the use of order_by

Try switching the order, and also specifying the field:

Subjects.objects.order_by('created_by', '-num_of_followers')\
    .distinct('created_by')[:5]

Upvotes: 2

Related Questions