gpt4 book ai didi

python - Django : ManyToMany relation error ( object has no attribute)

转载 作者:行者123 更新时间:2023-11-30 22:44:07 24 4
gpt4 key购买 nike

我试图在 Django 中定义一些 ManyToMany 关系,但是当我尝试创建相关对象时出现错误。

我的models.py:

class PerfumeBrand(models.Model):
class Meta:
verbose_name = "Marque de parfum"
verbose_name_plural = "Marques de parfum"

name = models.CharField(max_length=32)

def __str__(self):
return self.person.first_name + ' ' + self.person.last_name.upper() + ': ' + self.name


class Person(models.Model):
class Meta:
verbose_name = "Personne"
verbose_name_plural = "Personnes"

first_name = models.CharField(max_length=32, )
last_name = models.CharField(max_length=32)
email = models.EmailField(unique=True)
# Here is the ManyToMany relation
perfume_brands = models.ManyToManyField(PerfumeBrand)

问题是,当我尝试在数据库中保存 PerfumeBrand 对象时,出现此错误:

AttributeError at /admin/visualize/perfumebrand/add/

'PerfumeBrand' object has no attribute 'person'

我尝试在代码和管理面板中保存,出现相同的错误。

由于这是我第一次在 Django 项目中需要 ManyToMany 关系,所以我有点困惑,并且没有找到任何解决方案来解决我的问题。

你有什么想法吗?

最佳答案

PerfumeBrand从 undefined variable person,仅定义name

关于python - Django : ManyToMany relation error ( object has no attribute),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41611065/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com