Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.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 Can';无法访问instancemethod属性?_Python_Django_Python 2.7_Methods_Attributes - Fatal编程技术网

Python Can';无法访问instancemethod属性?

Python Can';无法访问instancemethod属性?,python,django,python-2.7,methods,attributes,Python,Django,Python 2.7,Methods,Attributes,是否有可能以某种方式访问方法的变量或属性 在Django中(但问题可能并不局限于Django): 应产生: <last_average_price>1548.48</last_average_price> 你知道怎么做才能让它工作吗?或者一些解决方法?为什么不返回完整的XML行 def get_last_average_price(self): return '<last_average_price>{}</last_average_price

是否有可能以某种方式访问方法的变量或属性

在Django中(但问题可能并不局限于Django):

应产生:

<last_average_price>1548.48</last_average_price>

你知道怎么做才能让它工作吗?或者一些解决方法?

为什么不返回完整的XML行

def get_last_average_price(self):
    return '<last_average_price>{}</last_average_price>'.format(value)

问题就在这里:

def get_last_average_price(self):
    self.get_last_average_price.label = 'Last AVG price'
这里
self.get\u last\u average\u price
引用了一个实例方法,您没有调用它。调用它就是将其编码为self.get\u last\u average\u price(),不幸的是,这也行不通,因为它会导致无限递归。我认为数据结构需要彻底改变

def get_last_average_price(self):
        prices = [x.get_last_scan().price for x in self.get_occurences() if x.get_last_scan() and x.get_last_scan().price]
        value = round(decimal.Decimal(sum(prices)/len(prices)),2) if prices else None
        return {"label": 'Last AVG price', "xml_tag": 'last_average_price', 'value': value }

请注意,您实际上并不需要xml\u close\u标记。因为这样就可以派生标记(请注意,我已经删除了<和>,让它们在下一阶段追加。

因为这种方法使用的不仅仅是生成XML。它有很多用途。它必须返回十进制(price)。是的,这可能是最好的方法。为什么你说我不需要xml_close_标记?因为它可以派生。但这需要对返回数据做一点更改。请参阅UpdateEyes,我考虑过这个选项,但不幸的是,PyCharm(或Django)不喜欢。那只是pycharm弄混了。我认为这不会导致任何django错误。
def get_last_average_price(self):
    return '<last_average_price>{}</last_average_price>'.format(value)
{% for method in product.statistic %}
    {{ method }}
{% endfor %}
def get_last_average_price(self):
    self.get_last_average_price.label = 'Last AVG price'
def get_last_average_price(self):
        prices = [x.get_last_scan().price for x in self.get_occurences() if x.get_last_scan() and x.get_last_scan().price]
        value = round(decimal.Decimal(sum(prices)/len(prices)),2) if prices else None
        return {"label": 'Last AVG price', "xml_tag": 'last_average_price', 'value': value }