django , list of models to list of string
I have a list of model objects:
just an example (I'm not really doing a list of all()):
class Tag(models.Model):
name = models.CharField(max_length=50,primary_key=True)
#Some other fields....
def __str__(self):
return self.name
mylist = list(Tag.objects.all())
What's the best way of converting this to a list of strings ?
Do I have to iterate the list ?I was doing something like:
newList = [ t.str(开发者_开发知识库) for t in mylist ]
Any better way ?
This
newList = [ str(t) for t in mylist ]
or
newList = map( str, myList )
But generally, we don't bother. We just leave it to the template to convert the query set into strings when the template is rendered.
精彩评论