![](/img/trans.png)
[英]DJANGO:How to remove empty QuerySet from the list of QuerySet
[英]How to remove MSSQL decimal declaration from QuerySet list?
我有一個排序算法,用於與父母及其子女一起排序。 這是一個工作示例: 教程點編碼地面 。
但是,此工作示例具有預定義的“已清除”列表。 但是,與該預定義列表的唯一區別是,我已從列表中刪除了“十進制聲明”(或任何稱為“十進制聲明”)。 這是臟列表和干凈列表的示例。
臟物清單:
item_list = [
{...},
{'qmaQuoteAssemblyID': 0, 'qmaLevel': Decimal('1'), 'qmaParentAssemblyID': 0, 'qmaPartShortDescription': '12U x 600 x 650 Professional Rack', 'qmaPartID': 'RACK S9', 'qmaQuantityPerParent': Decimal('1.00000')},
{...}]
清除清單:
item_list = [
{...},
{'qmaQuoteAssemblyID': 0, 'qmaLevel': 1, 'qmaParentAssemblyID': 0, 'qmaPartShortDescription': '12U x 600 x 650 Professional Rack', 'qmaPartID': 'RACK S9', 'qmaQuantityPerParent': 1.00000},
{...}]
從兩個示例中都可以看到, 臟列表的DecimalField值周圍有Decimal('somevalue')
。 但是,通過清除列表,它已被刪除,從而可以毫無問題地通過算法。
當我通過算法運行臟列表時 ,出現以下錯誤:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
NameError: name 'Decimal' is not defined
在我的Django項目中,該列表只是空的。
這是有道理的,因為未定義Decimal。 但是我的環境似乎將Decimal('somevalue')
當作方法/函數,而不是原始的十進制值。 我相信這個“垃圾”是在我的MSSQL數據庫或其他一些我與Django項目並行運行的Django程序周圍添加的值。
我目前這樣創建列表:
con1 = Q(qmaQuoteID=quote_id)
con2 = Q(qmaQuoteLineID=quote_line_id)
query = QuoteAssemblies.objects.filter(con1 & con2
).order_by('qmaQuoteAssemblyID'
).values('qmaQuoteAssemblyID', 'qmaParentAssemblyID', 'qmaLevel', 'qmaPartID', 'qmaPartShortDescription', 'qmaQuantityPerParent')
item_list = list(query)
有沒有一種方法可以清除列表以從值中刪除Decimal('somevalue')
,以便它正確地通過我的算法?
避免
raceback (most recent call last):
File "<stdin>", line 2, in <module>
NameError: name 'Decimal' is not defined
只是
from decmal import Decimal
如果仍然無法解決,並且想將小數轉換為int或字符串,
for item in item_list:
item['qmaLevel'] = int(item['qmaLevel'])
# item['qmaLevel'] = str(item['qmaLevel'])
我發現這是處理Decimal('X')
作為方法/函數被撤銷的最好方法。 我只是將Decimal
聲明為一個類,並確保python僅將其視為值,並且實質上無視周圍的垃圾:
class Decimal:
def __init__(self, value):
self.value = value
def value(self):
print(self.value)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.