Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Newbie ManyRelated Manager not Iterable Question

I'm trying to create a product code (in the admin) by combining elements from two other fields - one of which is a ManyToManyField. I'd like to iterate through that field to find out if a specific product option has been chosen, and append a variation of it to that non-editable product code, like so:

class ShirtColorClass(models.Model):
    shirtcolor = models.CharField(_('Shirt Color'), unique=True, max_length=40)
    def __unicode__(self):
        return self.shirtcolor

class ShirtClass(models.Model):
    shirtmodel = models.CharField(_('Model of Shirt'), max_length=40)
    shirtclr = models.ManyToManyField(_(ShirtColorClass, verbose_name='Shirt Color'))
    shirtcode = models.CharField(_('Code for the shirt'), max_length=80, editable=False)
    #...10 more fields...
    def __unicode__(self):
        return self.shirtmodel
    def save(self):
        for item in self.shirtclr: #these are the lines I'm not sure how to do
            if 'Blue' in self.shirtclr:
                self.shirtcode = u'%s%s' % ('B', self.shirtmodel)
            else:
                self.shirtcode = self.shirtmodel
            super(ShirtClass,self).save()

At the moment I'm getting a ManyRelatedManager not Iterable message, so I know I'm doing something wrong, but I don't know what... I apologize in advance for this being a stupid newbie question. Thank you.

like image 378
bkev Avatar asked Jan 13 '10 19:01

bkev


1 Answers

Try calling .all() on it.

like image 83
Ignacio Vazquez-Abrams Avatar answered Nov 09 '22 15:11

Ignacio Vazquez-Abrams