Django:尝试访问 ManyToMany 字段时出错
Django: error while trying to access ManyToMany field
我正在尝试创建一个类似 Twitter 的应用程序作为练习项目,并遵循此 video tutorial。我在我的应用程序中创建了以下模型。
class Profile(models.Model):
user = models.OneToOneField(to=User, on_delete=models.CASCADE)
bio = models.CharField(max_length=160, blank=True)
profile_photo = models.ImageField(blank=True, null=True)
followers = models.ManyToManyField("self", through="Relationship", related_name="follow_to",
symmetrical=False, blank=True)
class Relationship(models.Model):
user_followed = models.ForeignKey("User", related_name="followed", on_delete=models.CASCADE)
followed_by = models.ForeignKey("Profile", related_name="follower", on_delete=models.CASCADE)
timestamp = models.DateTimeField(auto_now=True)
迁移 运行 成功。
在此之后进行测试,我创建了两个用户(user1 和 user2)及其各自的个人资料,并让 user2 跟随 user1。这是它的代码-
Relationship.objects.create(user_followed=user1,followed_by=user2.profile)
此关系已成功创建。我尝试使用以下代码来获取 user1-
的关注者
user1.profile.followers.all()
但是上面的代码给出了以下错误-
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/mayank/.pyenv/versions/dwitter/lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py", line 535, in __get__
return self.related_manager_cls(instance)
File "/Users/mayank/.pyenv/versions/dwitter/lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py", line 821, in __init__
self.target_field_name = rel.field.m2m_reverse_field_name()
File "/Users/mayank/.pyenv/versions/dwitter/lib/python3.8/site-packages/django/db/models/fields/related.py", line 1554, in _get_m2m_reverse_attr
return getattr(self, cache_attr)
AttributeError: 'ManyToManyField' object has no attribute '_m2m_reverse_name_cache'
我试过here提到的方法,但它也给出了同样的错误。我不确定我在哪里犯了错误。如果有人能指正我,我将不胜感激。
因为你从 ManyToManyField
到 'self'
两个 ForeignKey
应该指向 Profile
:
class Relationship(models.Model):
<b>followed</b> = models.ForeignKey(
<b>'Profile'</b>,
related_name='followed',
on_delete=models.CASCADE
)
followed_by = models.ForeignKey(
<b>'Profile'</b>,
related_name='follower',
on_delete=models.CASCADE
)
timestamp = models.DateTimeField(auto_now=True)
因为现在两个 ForeignKey
都指向同一个模型,所以 source 字段和 target 字段是。您可以通过使用 through_fields=…
parameter [Django-doc]:
来解决这个问题
class Profile(models.Model):
user = models.OneToOneField(to=User, on_delete=models.CASCADE)
bio = models.CharField(max_length=160, blank=True)
profile_photo = models.ImageField(blank=True, null=True)
followers = models.ManyToManyField(
'self',
through='Relationship',
related_name='follow_to',
<b>related_fields=('followed', 'followed_by')</b>
symmetrical=False,
blank=True
)
因此,您可以创建一个关注者 Relation
:
Relationship.objects.create(
<b>followed</b>=<b>user1.profile</b>,
followed_by=user2.profile
)
我正在尝试创建一个类似 Twitter 的应用程序作为练习项目,并遵循此 video tutorial。我在我的应用程序中创建了以下模型。
class Profile(models.Model):
user = models.OneToOneField(to=User, on_delete=models.CASCADE)
bio = models.CharField(max_length=160, blank=True)
profile_photo = models.ImageField(blank=True, null=True)
followers = models.ManyToManyField("self", through="Relationship", related_name="follow_to",
symmetrical=False, blank=True)
class Relationship(models.Model):
user_followed = models.ForeignKey("User", related_name="followed", on_delete=models.CASCADE)
followed_by = models.ForeignKey("Profile", related_name="follower", on_delete=models.CASCADE)
timestamp = models.DateTimeField(auto_now=True)
迁移 运行 成功。
在此之后进行测试,我创建了两个用户(user1 和 user2)及其各自的个人资料,并让 user2 跟随 user1。这是它的代码-
Relationship.objects.create(user_followed=user1,followed_by=user2.profile)
此关系已成功创建。我尝试使用以下代码来获取 user1-
的关注者user1.profile.followers.all()
但是上面的代码给出了以下错误-
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/mayank/.pyenv/versions/dwitter/lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py", line 535, in __get__
return self.related_manager_cls(instance)
File "/Users/mayank/.pyenv/versions/dwitter/lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py", line 821, in __init__
self.target_field_name = rel.field.m2m_reverse_field_name()
File "/Users/mayank/.pyenv/versions/dwitter/lib/python3.8/site-packages/django/db/models/fields/related.py", line 1554, in _get_m2m_reverse_attr
return getattr(self, cache_attr)
AttributeError: 'ManyToManyField' object has no attribute '_m2m_reverse_name_cache'
我试过here提到的方法,但它也给出了同样的错误。我不确定我在哪里犯了错误。如果有人能指正我,我将不胜感激。
因为你从 ManyToManyField
到 'self'
两个 ForeignKey
应该指向 Profile
:
class Relationship(models.Model):
<b>followed</b> = models.ForeignKey(
<b>'Profile'</b>,
related_name='followed',
on_delete=models.CASCADE
)
followed_by = models.ForeignKey(
<b>'Profile'</b>,
related_name='follower',
on_delete=models.CASCADE
)
timestamp = models.DateTimeField(auto_now=True)
因为现在两个 ForeignKey
都指向同一个模型,所以 source 字段和 target 字段是。您可以通过使用 through_fields=…
parameter [Django-doc]:
class Profile(models.Model):
user = models.OneToOneField(to=User, on_delete=models.CASCADE)
bio = models.CharField(max_length=160, blank=True)
profile_photo = models.ImageField(blank=True, null=True)
followers = models.ManyToManyField(
'self',
through='Relationship',
related_name='follow_to',
<b>related_fields=('followed', 'followed_by')</b>
symmetrical=False,
blank=True
)
因此,您可以创建一个关注者 Relation
:
Relationship.objects.create(
<b>followed</b>=<b>user1.profile</b>,
followed_by=user2.profile
)