【发布时间】:2020-12-29 14:14:49
【问题描述】:
我正在使用 django 和 mysql。
假设有一个如下表:
class OrderInfo(models.Model):
gg_account_id = models.CharField(max_length=45, blank=True, null=True)
order_status = models.IntegerField(blank=True, null=True)
gg_status = models.IntegerField(blank=True, null=True)
uid = models.IntegerField(blank=True, null=True)
class Meta:
managed = False
db_table = 'order_info'
其中保存的数据是:
id gg_account_id order_status gg_status uid
1 6270491342 2 0 1
2 12321323 2 0 34
3 12321323 2 0 34
4 55551233 1 0 54
5 55551233 2 0 54
6 55551233 2 0 54
7 55551233 2 0 54
如果有多个具有相同gg_account_id 的数据,我只想获取其中一个。我的预期输出应该是:
1 6270491342 1
2 12321323 34
5 55551233 54
这是我对 orm 查询的试用:
recharge_account_list = OrderInfo.objects.\
filter(order_status=2, gg_status=0).\
distinct("gg_account_id").\
values_list("gg_account_id", "uid", "id")
print(recharge_account_list)
但我总是出错
File "D:\virtual\Envs\smb_middle_server\lib\site-packages\django\db\backends\base\operations.py", line 171, in distinct
_sql
raise NotSupportedError('DISTINCT ON fields is not supported by this database backend')
django.db.utils.NotSupportedError: DISTINCT ON fields is not supported by this database backend
我怎样才能得到预期的结果?
谢谢
【问题讨论】: