过滤掉我的查询集的重复项
Posted
技术标签:
【中文标题】过滤掉我的查询集的重复项【英文标题】:Filter out the repeated items of my queryset 【发布时间】:2019-10-16 17:20:36 【问题描述】:在我的 Django 项目中,当我查询一个 QuerySet 列表时,意外有 5 个相同的项目,您可以查看以下快照。
如果这是一个 Set,应该只有两个项目:(SE01-A3
, SE01-A4
)
这是我的测试代码的一部分:
qs = qs.annotate(letters=Substr('name', 1, 6),
length=Length('name')).order_by('letters', 'length', 'name') # you can ignore this sort code.
from django.forms.models import model_to_dict
for a_qs in qs:
jsonstr = model_to_dict(a_qs)
print(jsonstr)
print(jsonstr)
的输出:
'physical_server_model': 5, 'switchesport': 60, 'whmcs_tblorders_id': None, 'expire_time': datetime.datetime(2018, 6, 30, 16, 0, tzinfo=<UTC>), 'cabinet': 3, 'ram': 'Supermicro DDR4___16', 'ipmi_account': None, 'intranet_ip': None, 'cpu': 'Intel Xeon E3-1230 v5', 'task': None, 'has_intranet': False, 'user': 12, 'id': 26, 'price': Decimal('1000.00'), 'customer_desc': None, 'trade_record': 126, 'name': 'SE01-A3', 'pay_time': datetime.datetime(2018, 4, 30, 16, 0, tzinfo=<UTC>), 'switches': 3, 'ipmi_addr': '172.16.30.3', 'ipmi_pwd': None, 'desc': 'ip: 43.243.33.25 【As】', 'server_status': 'active', 'disk': 'Seagate SATA___1000'
'physical_server_model': 5, 'switchesport': 60, 'whmcs_tblorders_id': None, 'expire_time': datetime.datetime(2018, 6, 30, 16, 0, tzinfo=<UTC>), 'cabinet': 3, 'ram': 'Supermicro DDR4___16', 'ipmi_account': None, 'intranet_ip': None, 'cpu': 'Intel Xeon E3-1230 v5', 'task': None, 'has_intranet': False, 'user': 12, 'id': 26, 'price': Decimal('1000.00'), 'customer_desc': None, 'trade_record': 126, 'name': 'SE01-A3', 'pay_time': datetime.datetime(2018, 4, 30, 16, 0, tzinfo=<UTC>), 'switches': 3, 'ipmi_addr': '172.16.30.3', 'ipmi_pwd': None, 'desc': 'ip: 43.243.33.25 【As】', 'server_status': 'active', 'disk': 'Seagate SATA___1000'
'physical_server_model': 5, 'switchesport': 60, 'whmcs_tblorders_id': None, 'expire_time': datetime.datetime(2018, 6, 30, 16, 0, tzinfo=<UTC>), 'cabinet': 3, 'ram': 'Supermicro DDR4___16', 'ipmi_account': None, 'intranet_ip': None, 'cpu': 'Intel Xeon E3-1230 v5', 'task': None, 'has_intranet': False, 'user': 12, 'id': 26, 'price': Decimal('1000.00'), 'customer_desc': None, 'trade_record': 126, 'name': 'SE01-A3', 'pay_time': datetime.datetime(2018, 4, 30, 16, 0, tzinfo=<UTC>), 'switches': 3, 'ipmi_addr': '172.16.30.3', 'ipmi_pwd': None, 'desc': 'ip: 43.243.33.25 【As】', 'server_status': 'active', 'disk': 'Seagate SATA___1000'
'physical_server_model': 5, 'switchesport': 60, 'whmcs_tblorders_id': None, 'expire_time': datetime.datetime(2018, 6, 30, 16, 0, tzinfo=<UTC>), 'cabinet': 3, 'ram': 'Supermicro DDR4___16', 'ipmi_account': None, 'intranet_ip': None, 'cpu': 'Intel Xeon E3-1230 v5', 'task': None, 'has_intranet': False, 'user': 12, 'id': 26, 'price': Decimal('1000.00'), 'customer_desc': None, 'trade_record': 126, 'name': 'SE01-A3', 'pay_time': datetime.datetime(2018, 4, 30, 16, 0, tzinfo=<UTC>), 'switches': 3, 'ipmi_addr': '172.16.30.3', 'ipmi_pwd': None, 'desc': 'ip: 43.243.33.25 【As】', 'server_status': 'active', 'disk': 'Seagate SATA___1000'
'physical_server_model': 5, 'switchesport': 60, 'whmcs_tblorders_id': None, 'expire_time': datetime.datetime(2018, 6, 30, 16, 0, tzinfo=<UTC>), 'cabinet': 3, 'ram': 'Supermicro DDR4___16', 'ipmi_account': None, 'intranet_ip': None, 'cpu': 'Intel Xeon E3-1230 v5', 'task': None, 'has_intranet': False, 'user': 12, 'id': 26, 'price': Decimal('1000.00'), 'customer_desc': None, 'trade_record': 126, 'name': 'SE01-A3', 'pay_time': datetime.datetime(2018, 4, 30, 16, 0, tzinfo=<UTC>), 'switches': 3, 'ipmi_addr': '172.16.30.3', 'ipmi_pwd': None, 'desc': 'ip: 43.243.33.25 【As】', 'server_status': 'active', 'disk': 'Seagate SATA___1000'
'physical_server_model': 5, 'switchesport': 62, 'whmcs_tblorders_id': None, 'expire_time': None, 'cabinet': 3, 'ram': 'Supermicro DDR4___16', 'ipmi_account': None, 'intranet_ip': None, 'cpu': 'Intel Xeon E3-1230 v5', 'task': None, 'has_intranet': False, 'user': 12, 'id': 27, 'price': Decimal('1000.00'), 'customer_desc': None, 'trade_record': None, 'name': 'SE01-A4', 'pay_time': None, 'switches': 3, 'ipmi_addr': '172.16.30.4', 'ipmi_pwd': None, 'desc': '', 'server_status': 'active', 'disk': 'Seagate SATA___1000'
有没有办法过滤掉重复的项目,在我的情况下只留下我的要求(SE01-A3
,SE01-A4
)设置?
【问题讨论】:
Select DISTINCT individual columns in django?的可能重复 【参考方案1】:要跳过重复项,我们必须在 queryset
上使用 distinct
方法,如下所示
from django.forms.models import model_to_dict
qs = qs.annotate(
letters=Substr('name', 1, 6),
length=Length('name')
).order_by('letters', 'length', 'name').distinct()
for a_qs in qs:
jsonstr = model_to_dict(a_qs)
print(jsonstr)
【讨论】:
以上是关于过滤掉我的查询集的重复项的主要内容,如果未能解决你的问题,请参考以下文章