Iterate through Expando Dynamic Properties in Django Templates

I am trying to iterate through the dynamic properties of an Expando-Model to print all of them. Is there a way to do this differently than creating my own method, for example:

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 to the '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 surprised 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 my method call ... should I use something other than ".items"?

+3
source share
1 answer

db.Model 'dynamic_properties' Expando , '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 %}
0

Source: https://habr.com/ru/post/1757682/


All Articles