Django 新手 ManyRelated Manager 不可迭代的问题
我正在尝试通过组合其他两个字段的元素(其中之一是 ManyToManyField)来创建产品代码(在管理中)。我想遍历该字段以查明是否已选择特定产品选项,并将其变体附加到不可编辑的产品代码中,如下所示:
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()
目前我收到 ManyRelatedManager not Iterable 消息,所以我知道我做错了什么,但我不知道是什么......我提前为这是一个愚蠢的新手问题道歉。谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试对其调用
.all()
。Try calling
.all()
on it.调用
filter()
:您可以通过添加
default=''
来避免self.shirtcode = ''
call
filter()
:you can avoid
self.shirtcode = ''
by addingdefault=''
into