我想使用我的Django模型生成列表
说我有这些模型:
class AlarmServer(models.Model): ip = models.IPAddressField()
这样的清单
server_ips = [i.ipfor i in AlarmServer.objects.all()]
似乎没有用,我在做什么错?
server_ips = [i.ip for i in AlarmServer.objects.all()]
应该工作(我刚刚添加了一个空格)。我已经尝试过如下
mez@stupor % ./manage.py shell Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> from mysite_org.videos.models import Video >>> url_list = [v.url for v in Video.objects.all()] >>> url_list [u'http://media.mysite.org/videos/sblug_jan2009.flv', u'http://media.mysite.org/videos/sblug_feb2009.flv', u'http://media.mysite.org/videos/phpwm_mar2009.flv', u'http://media.mysite.org/videos/sblug_may2009.flv', u'http://media.mysite.org/videos/sblug_june2009.flv', u'http://media.mysite.org/videos/sblug_sep2009.flv', u'http://media.mysite.org/videos/bugjam-oct-2009.flv']