【问题标题】:How to retrieve items from a django queryset?如何从 django 查询集中检索项目?
【发布时间】:2012-11-26 17:38:52
【问题描述】:

我正在尝试在查询集中获取视频元素,但在检索它时遇到了问题。

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

模型.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[0].video吗?

标签: python django view model django-queryset


【解决方案1】:

user_channel[0] 不是字典。使用

user_channel[0].video

【讨论】:

    【解决方案2】:

    试试这个你会得到基于过滤器的视频列表

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-17
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      • 2012-01-04
      • 2018-02-09
      • 2021-10-29
      相关资源
      最近更新 更多