繁体   English   中英

如何从Django查询集中检索项目?

[英]How to retrieve items from a django queryset?

我正在尝试在queryset中获取视频元素,但是在检索它时遇到了麻烦。

user_channel = Everything.objects.filter(profile = request.user, playlist = 'Channel')
print user_channel[0] #returns the first result without error   
print user_channel[0]['video'] #returns error

Models.py:

class Everything(models.Model):
    profile = models.ForeignKey(User)
    playlist = models.CharField('Playlist', max_length = 2000, null=True, blank=True)
    platform = models.CharField('Platform', max_length = 2000, null=True, blank=True)
    video = models.CharField('VideoID', max_length = 2000, null=True, blank=True)
    video_title = models.CharField('Title of Video', max_length = 2000, null=True, blank=True)
    def __unicode__(self):
        return u'%s %s %s %s %s' % (self.profile, self.playlist, self.platform, self.video, self.video_title)

试试看,您会获得基于过滤器的视频列表

user_channel = Everything.objects.filter(profile = request.user, playlist = 'Channel')
video = [x.video for x in user_channel]
print video/print video[0]

user_channel[0]不是字典。 采用

user_channel[0].video

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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