Django ORM对象没有属性' _set'

时间:2014-07-21 21:36:10

标签: python django

我认为Django会自动为我创建_set,但我发现以下错误。

for dep_badge in self.badge.badge_set.all():
AttributeError: 'Badge' object has no attribute 'badge_set'

型号:

class Badge(RewardBase):
    """
    Represents a Badge.
    """
    name = models.CharField(max_length=60, help_text="Name of the badge")
    description = models.TextField(max_length=150)
    reputation_value = models.IntegerField(help_text="Value given in accumulate reputation", default=0)
    reputation_prerequisite = models.IntegerField(help_text="Point value needed to obtain", default=0)
    difficulty_level = models.IntegerField(choices=STATUS_TYPES, default=STATUS_EASY,
                                           help_text="Badge difficulty")


class Award(RewardBase):
    """
    Representation of a badge awarded to a user.
    """

    description = models.CharField(max_length=100, choices=STATUS_TYPES, default=STATUS_GENERAL)
    value = models.IntegerField(default=0)
    read = models.BooleanField(default=False, help_text="Has the user read this?")
    created = models.DateTimeField(blank=True, null=True, editable=False)
    hidden = models.BooleanField(default=False)

    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    badge = models.ForeignKey("Badge")


    def save(self, *args, **kwargs):


        super(Award, self).save(*args, **kwargs)


            for dep_badge in self.badge.badge_set.all():
                dep_badge.check_prerequisites(self.user, sel

f.badge,self)

2 个答案:

答案 0 :(得分:2)

徽章没有badge_set.No外键。也许你的意思是award_set?

答案 1 :(得分:1)

看起来你打算做

self.badge.award_set.all()

代替。 badge_set正在寻找与Badge模型的反向关系,该模型不存在。