开发者

django , list of models to list of string

开发者 https://www.devze.com 2023-03-02 16:40 出处:网络
I have a list of model objects: just an example (I\'m not really doing a list of all()): class Tag(models.Model):

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号