简体   繁体   English

如何从QuerySet列表中删除MSSQL十进制声明?

[英]How to remove MSSQL decimal declaration from QuerySet list?

I have a sorting algorithm that orders a list with parents and their children. 我有一个排序算法,用于与父母及其子女一起排序。 Here is a working example: Tutorials Point Coding Ground . 这是一个工作示例: 教程点编码地面

This working example, however, has a predefined "cleaned" list. 但是,此工作示例具有预定义的“已清除”列表。 The only difference however with this predefined list is that I have removed the "Decimal Declarations" (or whatever they are called) from the list. 但是,与该预定义列表的唯一区别是,我已从列表中删除了“十进制声明”(或任何称为“十进制声明”)。 Here is an example of both dirty and clean lists. 这是脏列表和干净列表的示例。

Dirty 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')},
    {...}]

Clean List: 清除清单:

item_list = [
    {...},
    {'qmaQuoteAssemblyID': 0, 'qmaLevel': 1, 'qmaParentAssemblyID': 0, 'qmaPartShortDescription': '12U x 600 x 650 Professional Rack', 'qmaPartID': 'RACK S9', 'qmaQuantityPerParent': 1.00000},
    {...}]

As you can see from both the examples that the dirty list has Decimal('somevalue') surrounding it's DecimalField value. 从两个示例中都可以看到, 脏列表的DecimalField值周围有Decimal('somevalue') However with the clean list it has that removed allowing it to go through the algorithm without a problem. 但是,通过清除列表,它已被删除,从而可以毫无问题地通过算法。

When I run the dirty list through the algorithm I get this error: 当我通过算法运行脏列表时 ,出现以下错误:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
NameError: name 'Decimal' is not defined

And within my Django project the list is simply considered empty. 在我的Django项目中,该列表只是空的。

This makes sense because Decimal isn't defined. 这是有道理的,因为未定义Decimal。 But my environemnt seems to be treating Decimal('somevalue') as a method/function instead of it's raw decimal value. 但是我的环境似乎将Decimal('somevalue')当作方法/函数,而不是原始的十进制值。 I believe this "junk" is added around the value either from my MSSQL database or some other MSSQL → Django programs I have running parallel with my Django project. 我相信这个“​​垃圾”是在我的MSSQL数据库或其他一些我与Django项目并行运行的Django程序周围添加的值。

I currently create the list like so: 我目前这样创建列表:

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)

Is there a way to clean the list to remove the Decimal('somevalue') from the value so it goes through my algorithm correctly? 有没有一种方法可以清除列表以从值中删除Decimal('somevalue') ,以便它正确地通过我的算法?

to avoid 避免

raceback (most recent call last):
  File "<stdin>", line 2, in <module>
NameError: name 'Decimal' is not defined

just 只是

from decmal import Decimal


if still does not work out and want to convert decimal to int or string, 如果仍然无法解决,并且想将小数转换为int或字符串,

for item in item_list:
    item['qmaLevel'] = int(item['qmaLevel'])
    # item['qmaLevel'] = str(item['qmaLevel'])

I found that this was the best way to deal with Decimal('X') being reprieved as a method/function. 我发现这是处理Decimal('X')作为方法/函数被撤销的最好方法。 I simply declared Decimal as a class and made sure python only sees it as a value and in essence disregard the junk around it: 我只是将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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM