迭代 Django 模板中的 Expando 动态属性

发布于 2024-09-12 03:42:38 字数 834 浏览 1 评论 0原文

我正在尝试迭代 Expando-Model 的动态属性,以便将它们全部输出。除了创建自己的方法之外,还有其他方法可以做到这一点吗:

class Event(db.Expando):
    platform = db.ReferenceProperty(Platform)
    date = db.DateTimeProperty()

    def getValues(self):
        return self._dynamic_properties

然后在模板中 - 传递一个“平台”对象:

{% for event in platform.event_set %}
    <b>{{ event.date }}</b><br />
    {% for pair in event.getValues.items %}
        {{ pair.0 }} = {{ pair.1 }}<br />
    {% endfor %}
{% endfor %}

这可行,但我很惊讶我不能这样做:

{% for event in platform.event_set %}
    <b>{{ event.date }}</b><br />
    {% for pair in event.items %}
        {{ pair.0 }} = {{ pair.1 }}<br />
    {% endfor %}
{% endfor %}

没有我自己的方法方法调用...我应该使用“.items”以外的其他内容吗?

I'm trying to iterate through an Expando-Model's dynamic properties in order to output them all. Is there a way of doing this other than creating your own method like such:

class Event(db.Expando):
    platform = db.ReferenceProperty(Platform)
    date = db.DateTimeProperty()

    def getValues(self):
        return self._dynamic_properties

And then in the template - which is passed a 'platform' object:

{% for event in platform.event_set %}
    <b>{{ event.date }}</b><br />
    {% for pair in event.getValues.items %}
        {{ pair.0 }} = {{ pair.1 }}<br />
    {% endfor %}
{% endfor %}

This works, but I'm suprised I can't just do:

{% for event in platform.event_set %}
    <b>{{ event.date }}</b><br />
    {% for pair in event.items %}
        {{ pair.0 }} = {{ pair.1 }}<br />
    {% endfor %}
{% endfor %}

Without having my own method call... should I use something other than '.items'?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小情绪 2024-09-19 03:42:43

您可以使用 db.Model 的“dynamic_properties”方法来检索 Expando 的属性,格式与“properties”方法相同(如 在 Model 类下

请记住,这些函数不返回属性的值,但...“properties”返回属性名称的字典映射到其实现类(在本例中为 db.ReferenceProperty 和 db.DateTimeProperty),而“dynamic_properties”仅返回属性名称列表,因为 Expandos 无法将动态值映射到实现类。

为了获取属性的值,您必须使用getattr(model, prop_name)。没有办法纯粹在 Django 模板内运行此函数(没有 自定义标签库),但您可以像这样保留并重写您的方法......

def getExpandoValues(self):
   return dict((name, getattr(self, name)) for name, impl_class in self.dynamic_properties())

并重写您的模板以通过新的字典输入访问值:

{% for event in platform.event_set %}
  <b>{{ event.date }}</b><br />
  {% for pair in event.getExpandoValues %}
     {{ pair.0 }} = {{ pair.1 }}<br />
  {% endfor %}
{% endfor %}

You can use a db.Model's 'dynamic_properties' method to retrieve an Expando's properties, in the same format as the 'properties' method (as documented under the Model class)

Remember that those functions don't return a property's value, though... 'properties' returns a dictionary of property names mapped to their implementation class (db.ReferenceProperty and db.DateTimeProperty, in this case), and 'dynamic_properties' simply returns a list of property names, since Expandos can't map a dynamic value to an implementation class.

In order to get the property's value, you have to use getattr(model, prop_name). There is no way to run this function purely inside a Django template (without a custom tag library), but you could keep and re-write your method like so...

def getExpandoValues(self):
   return dict((name, getattr(self, name)) for name, impl_class in self.dynamic_properties())

... and re-write your template to access the values via the new dictionary input:

{% for event in platform.event_set %}
  <b>{{ event.date }}</b><br />
  {% for pair in event.getExpandoValues %}
     {{ pair.0 }} = {{ pair.1 }}<br />
  {% endfor %}
{% endfor %}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文