Here’s my code:
[{{thing.syncedthing_set.all.0.state}}]
{% ifequal thing.syncedthing_set.all.0.state "InProgress" %}
It worked
{% endifequal %}
The result:
[InProgress]
In other words, the variable lookup works, but isn’t passing the ‘ifequal’ test. I can’t think of any reason this wouldn’t work other than variable lookups not being supported.
There’s a mailing list message from 2007 saying they weren’t supported back then: https://groups.google.com/forum/?fromgroups#!topic/django-users/oU1im3UTcLc – is this still the case?
If so, any good workarounds? It’s not easy in this case to modify the Python code that calls the template.
If they ought to work – any other possible causes?
There’s this very similar question but the answer doesn’t work for me. I’m using Django 1.3.
EDIT
Ok, so the ‘state’ has type class. Looking up the definition, state refers to a class which eventually subclasses models.Field:
class FSMField(models.Field):
__metaclass__ = models.SubfieldBase
states = {}
""" Contains { 'Statename': StateClass, ... }"""
def __init__(self, *args, **kwargs):
kwargs.setdefault('max_length', 50)
super(FSMField, self).__init__(*args, **kwargs)
def to_python(self, value):
if isinstance(value, State):
return value
return self.__class__.states.get(str(value))()
def get_prep_value(self, value):
return str(value)
def get_internal_type(self):
return 'CharField'
Given that – I’m actually not sure how to get a real string value in the templating system. Forgive my newbishness.
EDIT2
Actually, the relevant bit is probably the ‘State’ class itself:
class State(object):
""" A simple finite statement machine abstract class, with on_entry and on_exit events."""
def __unicode__(self):
return "%s" % (self.__class__.__name__)
def __str__(self):
return "%s" % (self.__class__.__name__)
There’s a difference between an object’s string representation, and what it actually is. It seems likely that your
stateproperty returns an object whose unicode representation isInProgress, but that doesn’t make it actually equal to"InProgress". This would be true for example ifstateis a ForeignKey to another model – in which case you could just add another level to the lookup to get to the actual field that returns that status.Edit after update I think you’ve coded yourself into a corner, unfortunately. Since there’s no actual instance attribute that contains the state as a string, there’s no way to get it via string comparison.
A couple of possibilities spring to mind. One is to pass all possible
Statesubclasses to the template context (perhaps via a context processor), after which you’ll be able to compare yourstatewith the actual objects. This is pretty horrible.An alternative would be to add a function, either on the
Stateclass or possibly on the model, which gets the state as a string. This could be as simple as the existing__unicode__method (you can’t actually use that, because the template language forbids accessing attributes that begin with underscores). Then you can doifequal foo.bar.0.state.as_string "InProgress"or whatever, and that will call theas_string()method and your comparison will succeed.