django - accessing python object list -


i'm trying retrieve list of images in directory, , store list attribute of class. when try list object appears empty.this class:

class hunting(models.model):     code = models.charfield(max_length=10)      description = models.charfield(max_length=500)     images = {}      def __str__(self):         res = ''         image in self.images:             res = res+image         return res      def __unicode__(self):         return self.id 

and populate , read list:

page = int(page) huntings = paginator(hunting.objects.all(), 9) images = {} hunting in huntings.page(page):     dir_name = settings.images_root+'\\theme1\\images\\cotos\\'+str(hunting.id)+'\\'  # insert path directory     path = os.path.join(settings.static_url, dir_name)     print >>sys.stderr, '--path:' + path     if os.path.exists(path):            print >>sys.stderr, '--list image hunting '+str(hunting.id)+'--'         img_list = os.listdir(path)         hunting.images=img_list         print >>sys.stderr, hunting.images hunting in huntings.page(page):     print >>sys.stderr, '*******hunting '+str(hunting.id)+':'     print >>sys.stderr, hunting.images 

so, when iterating first time , printing hunting.images list got:

--list image hunting 2-- ['1_tn.jpg', '2_tn.jpg', '3_tn.jpg', '4_tn.jpg'] --list image hunting 4-- ['coto10.png', 'coto11.png', 'coto12.png'] 

but second iteration, trying list stored before empty list

*******hunting 2: {} *******hunting 4: {} 

i think happened call huntings.page(page) both of loops. first loop did assign images result of huntings.page(page), didn't keep result, second loop call huntings.page(page) again , got same initial result before.

i'm not sure want achieve here, need return value first in variable update it:

# keep return value in variable huntings huntings = [for hunting in huntings.page(page)] hunting in huntings:     # hunting.images = img_list  hunting in huntings:     print >>sys.stderr, hunting.images 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -