Django 新手 ManyRelated Manager 不可迭代的问题

发布于 2024-08-17 13:59:14 字数 1082 浏览 2 评论 0原文

我正在尝试通过组合其他两个字段的元素(其中之一是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

电影里的梦 2024-08-24 13:59:14

尝试对其调用 .all()

Try calling .all() on it.

命比纸薄 2024-08-24 13:59:14

调用 filter()

def save(self):
    if self.pk!=None:
        if self.shirtclr.filter(shirtcolor='Blue'):
            self.shirtcode = u'%s%s' % ('B', self.shirtmodel)
        else:
            self.shirtcode = self.shirtmodel
    else:
        self.shirtcode = ''

您可以通过添加 default='' 来避免 self.shirtcode = ''

shirtcode = models.CharField(_('Code for the shirt'), max_length=80, editable=False, default='')

call filter():

def save(self):
    if self.pk!=None:
        if self.shirtclr.filter(shirtcolor='Blue'):
            self.shirtcode = u'%s%s' % ('B', self.shirtmodel)
        else:
            self.shirtcode = self.shirtmodel
    else:
        self.shirtcode = ''

you can avoid self.shirtcode = '' by adding default='' into

shirtcode = models.CharField(_('Code for the shirt'), max_length=80, editable=False, default='')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文