Django MySQL distinct query for getting multiple values

后端 未结 2 1940
陌清茗
陌清茗 2020-12-09 19:37

I have a MySQL database unfortunately used with Django 1.4.1. Distinct function is only working for POSTGRESQL if i get it right.

相关标签:
2条回答
  • 2020-12-09 19:50

    .distinct([*fields]) only works in PostgresSQL.

    From distinct documentation

    Here's the difference. For a normal distinct() call, the database compares each field in each row when determining which rows are distinct. For a distinct() call with specified field names, the database will only compare the specified field names.

    As stated all fields in a record are checked. Mostly likely in your case you are getting records with different field values (more likely a case if you are queries multiple tables ManyToMany or ForeignKey relations).

    For consolidating as array you can refer your earlier question Django Query distinct values works but i cant use the query result

    0 讨论(0)
  • 2020-12-09 20:01
    names = Staff.objects.order_by('person__full_name').values('person__full_name').distinct()
    

    will give you distinct full names, and you can do a similar thing to get distinct job categories.

    These will give you lists of values, not objects themselves, but if I interpret your question correctly then I think these will give you what you want.

    0 讨论(0)
提交回复
热议问题