sharataka
sharataka

Reputation: 5132

How to retrieve items from a django queryset?

I'm trying to get the video element in a queryset but am having trouble retrieving it.

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)

Upvotes: 1

Views: 260

Answers (2)

kartheek
kartheek

Reputation: 6684

Try this you get list of videos based on filter

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

Upvotes: 1

Matthias
Matthias

Reputation: 13222

user_channel[0] is not a dictionary. Use

user_channel[0].video

Upvotes: 0

Related Questions