В моем проекте Django, когда я запрашиваю список QuerySet, неожиданно появилось 5 одинаковых элементов, вы можете проверить приведенный ниже снимок.
Если это набор, то должно быть только два элемента: (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
) установлено?