Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django+甲骨文+订单_Python_Django_Oracle - Fatal编程技术网

Python Django+甲骨文+订单

Python Django+甲骨文+订单,python,django,oracle,Python,Django,Oracle,我在使用Django ORM对QuerySet中的元素进行排序时遇到了一些问题 这是我的模型: 项目: 属性: class Attribute(models.Model): name = models.CharField(max_length=128) 价值: class Value(models.Model): value = models.TextField() attr = models.ForeignKey(Attribute, related_name='at

我在使用Django ORM对QuerySet中的元素进行排序时遇到了一些问题

这是我的模型:

项目:

属性:

class Attribute(models.Model):
    name = models.CharField(max_length=128)
价值:

class Value(models.Model):
    value = models.TextField()
    attr = models.ForeignKey(Attribute, related_name='attr2value')
    item = models.ForeignKey(Item, related_name='item2value')
    start_date = models.DateTimeField(auto_now_add=True)
    end_date = models.DateTimeField(null=True, blank=True)
假设3个项目的属性名称和折扣都有值。 如何获取具有“折扣”属性值且该值的“结束日期”字段大于“现在”的所有项目,并根据“名称”属性的值对项目进行排序

谢谢。

这是解决方案

 def sortByAtt(attribute, order="ASC", type="str"):
          #IMPORTANT: should be called before any filter

    '''
        You should change the field type if it's Text field
        CORE_VALUE - Table name
        VALUE - Field name
    '''
    if type != "str":
        case = 'TO_NUMBER("CORE_VALUE"."VALUE", \'999999999.999\')'
    else:
        case = 'CAST("CORE_VALUE"."VALUE" AS VARCHAR(100))'

    if order != "ASC":
        case = '-' + case

    return Item.objects.filter(item2value__attr__title=attribute).extra(order_by=[case])


#Sould be written on different lines(Django ORM logic)
items = sortByAtt("NAME")
items.filter(#ANOTHER FILTER)
解决方案2

 def sortByAtt(attribute, order="ASC", type="str"):
          #IMPORTANT: should be called before any filter

    '''
        You should change the field type if it's Text field
        CORE_VALUE - Table name
        VALUE - Field name
    '''
    if type != "str":
        case = 'TO_NUMBER("CORE_VALUE"."VALUE", \'999999999.999\')'
    else:
        case = 'CAST("CORE_VALUE"."VALUE" AS VARCHAR(100))'

    if order != "ASC":
        case = '-' + case

    return Item.objects.filter(item2value__attr__title=attribute).extra(order_by=[case])


#Sould be written on different lines(Django ORM logic)
items = sortByAtt("NAME")
items.filter(#ANOTHER FILTER)
def sortQuerySetByAttr(queryset, attribute, order="ASC", type="str"):
    '''
        Order Items by attribute
        queryset: QuerySet
        attribute: Attribute name
        order: Order direction DESC / ASC
        type: Attribute value type str/int
            Example: qSet = Item.objects.filter(#Many filters)
            qSet = sortQuerySetByAttr(qSet, "NAME", "DESC", "int")[:15]
    '''


    '''
        You should change the field type if it's Text field
        CORE_VALUE - Table name
        VALUE - Field name
    '''

    if type != "str":
        case = 'TO_NUMBER("CORE_VALUE"."TITLE", \'999999999.999\')'
    else:
        case = 'CAST("CORE_VALUE"."TITLE" AS VARCHAR(100))'

    if order != "ASC":
        case = '-' + case

    return queryset.model.objects.filter(pk__in=queryset, item2value__attr__title=attribute).extra(order_by=[case])