简体   繁体   中英

Querying a list

objects = object.objects.values('column name')

In HTML, this prints as

['column name':"[item1, item2, item3']"}]

Is it possible to access just the elements so rather than the item showing the above in HTML it just shows

item1 item2 item3

?

Your queryset returns a list, so you need to iterate over the objects within the list, and also specify which field to print.

{% for object in objects %}
    {{ object.column_name }}
{% endfor %}

Additionally, in your model, you can specify a value to return if you just call {{ object }} .

class testResult(models.Model):
    # define your model fields here

    def __str__(self): # assuming python 3
        return self.column_name

... in which case the first example could be achieved by doing:

{% for object in objects %}
    {{ object }}
{% endfor %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM