Django - 可以迭代方法吗? [英] Django - is it possible to iterate over methods?
问题描述
编辑: $ b $我正在Django的Web应用程序上工作,该应用程序适用于产品,价格和统计信息等。 b更明显的解释:如何组合或标记一些实例方法,以便我可以在中迭代它们,例如instance.name_of_the_group中的方法
为了保持简单 - 我有一个模型产品
。 产品
具有多种属性和方法。其中一些方法返回统计数据。
class Product(models.Model):
name =
...
def save(...
...
def get_all_colors(self):
...
def get_historical_average_price(self):#statistic method
price =< some calculate>
return price
def get_historical_minimal_price(self):#统计方法
...
返回价格
所以有很多方法如 get_historical_average_price
和 get_historical_minimal_price
。
现在我有编写标签并在项目中逐个调用这些方法,例如生成表或创建XML导出时。
我想要能够以某种方式标记他们那些是统计的方法,给他们一些名字,所以我可以使用为
循环e tc。
有没有办法做到这一点?
XML生成器示例:
< products>
{%for product in products%}
< product>
< code> {{product.code}}< / code>
< name> {{product.name}}< / name>
< statistics>
< history_average> {{product.get_historical_average_price}}< / history_average>
< minimal_price> {{product.get_historical_minimal_price}}< / minimal_price>
< / statistics>
< / product>
{%endfor%}
< / products>
所以我会这样做:
<统计>
{%for product.statistics%}中的statistic_method
{{statistic_method.name}}> {{statistic_method}}< / {{statistic_method.name}}>
{%endfor%}
< / statistics>
而不是:
<统计>
< history_average> {{product.get_historical_average_price}}< / history_average>
< minimal_price> {{product.get_historical_minimal_price}}< / minimal_price>
< / statistics>
这是使用自定义模型管理器,您可以使用或覆盖他们在那里使用的名称。
所以在你的例子中,例如:
class StatisticsManager model.Manager):
def history_average_price(self):
...
class产品(models.Model):
name = ...
statistics = StatisticsManager()
然后以 b
product_price = Product.statistics.get_historical_average_price
等等。
编辑:
我忘了 - 当你覆盖默认的 / code>经理,我相信您需要根据这篇文章 - 但是如果需要,你可以在对象上有多个管理员,所以
对象
, statistics
, otherstuffhere
。
I'm working on a Web Application in Django which works with products, prices and statistics etc.
EDIT:
More straighforward explanation: How to "group" or "mark" some of instance methods so I can iterate over them like for method in instance.name_of_the_group
To keep it simple - I have a model Product
. Product
has multiple attributes and methods. Some of those methods returns "statistics" data.
class Product(models.Model):
name = ...
...
def save(...
...
def get_all_colors(self):
....
def get_historical_average_price(self): #statistic method
price = <some calculation>
return price
def get_historical_minimal_price(self): #statistic method
...
return price
So there is a lot of methods like get_historical_average_price
and get_historical_minimal_price
.
Now I have to write labels and call these methods one by one in the project. For example when I generate a table or creating an XML export.
I would like to be able to somehow "mark" them that those are "statistic" methods, give them some name so I would be able to work with them using for
loops etc.
Is there some way to do that?
Example on XML generator:
<products>
{% for product in products %}
<product>
<code>{{ product.code }}</code>
<name>{{ product.name }}</name>
<statistics>
<historical_average>{{ product.get_historical_average_price}}</historical_average>
<minimal_price>{{ product.get_historical_minimal_price}}</minimal_price>
</statistics>
</product>
{% endfor %}
</products>
So I would do something like:
<statistics>
{% for statistic_method in product.statistics %}
<{{ statistic_method.name }}>{{ statistic_method }}</{{ statistic_method.name }}>
{% endfor %}
</statistics>
instead of:
<statistics>
<historical_average>{{ product.get_historical_average_price}}</historical_average>
<minimal_price>{{ product.get_historical_minimal_price}}</minimal_price>
</statistics>
This is a great use case for using custom model managers as you can use or override the names that they use there.
So in your example, something like :
class StatisticsManager(models.Manager):
def historical_average_price(self):
...
class Product(models.Model):
name = ...
statistics = StatisticsManager()
Which would then be called in the form
product_price = Product.statistics.get_historical_average_price
and so on.
Edit:
I forgot - as you're overriding the default objects
manager, I believe that you'll need to explicitly restate that as a manager, per this article - but you can have multiple managers on an object if desired, so objects
, statistics
, otherstuffhere
.
这篇关于Django - 可以迭代方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!