过滤掉查询集中的重复项

时间:2019-05-31 04:20:15

标签: python django django-queryset

在我的Django项目中,当我查询QuerySet列表时,意外有5个相同的项目,您可以检查以下快照。
如果是集合,则应该只有两项:(SE01-A3SE01-A4

enter image description here

这是我的测试代码的一部分:

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-A3SE01-A4)?

1 个答案:

答案 0 :(得分:0)

要跳过重复项,我们必须在distinct上使用queryset方法,如下所示

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)