ajwood
ajwood

Reputation: 19027

Displaying unicode in a django template

I feel there's an obvious answer to this...

I've got a list of unicode values that I want to use in a django template.

The models...

# models.py
class MyModel( models.Model ):
    # ...
    def my_char_fields(self):
        return AnotherModel.objects.filter(mymodel=self.pk).values_list('cf').distinct()

class AnotherModel( models.Model ):
    # ...
    cf = models.CharField( max_length=6 )
    mymodel = ForeignKey(MyModel)

And in my template...

#MyTemplate.html
<ul>
{% for cf in mymodel.my_char_fields %}
<li>cf</li>
{% endfor %}
</ul>

The result is stuff like:

but it should be:

Upvotes: 1

Views: 1542

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798526

You forgot to pass flat=True to .values_list().

Upvotes: 3

Related Questions