【问题标题】:Multiple many to many relationships with the same two classes - Django具有相同两个类的多个多对多关系 - Django
【发布时间】:2018-10-05 09:25:19
【问题描述】:

我有两个类,它们之间有两个多对多关系。由于相邻的表还必须具有其他属性,因此我明确定义了它们。但我收到一条错误消息,提示我需要更改相关名称。不知道在哪里做这个。非常感谢任何帮助。

这是我的 models.py 代码:

class Client(models.Model):
    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255)
    email = models.CharField(max_length=255)
    password = models.CharField(max_length=255)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    objects = UserManager()

    def __repr__(self):
        return "<User object: {} {} {} {}>".format(self.first_name, 
self.last_name, self.email, self.password)

class Therapist(models.Model):
    name = models.CharField(max_length=255)
    reviews = models.ManyToManyField(Client, through="Review")
    appts = models.ManyToManyField(Client, through="Appointment")
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    objects = UserManager()

    def __repr__(self):
        return "<Book object: {}>".format(self.title)



class Review(models.Model):
    reviewer = models.ForeignKey(Client, on_delete=models.CASCADE)
    therapist_reviewed = models.ForeignKey(Therapist, 
on_delete=models.CASCADE)
    rating = models.IntegerField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    content = models.TextField()

    def __repr__(self):
        return "<Review object: {} {}>".format(self.book_reviewed, 
self.reviewer)

class Appointment(models.Model):
    booked_therapist = models.ForeignKey(Therapist, 
on_delete=models.CASCADE)
    booked_client = models.ForeignKey(Client, on_delete=models.CASCADE)
    date = models.DateTimeField()
    massage = models.CharField(max_length=255)

【问题讨论】:

  • 请分享回溯
  • 错误:application.Therapist.appts:(fields.E304)“Therapist.appts”的反向访问器与“Therapist.reviews”的反向访问器冲突。提示:在“Therapist.appts”或“Therapist.reviews”的定义中添加或更改related_name 参数。 application.Therapist.reviews:(fields.E304)“Therapist.reviews”的反向访问器与“Therapist.appts”的反向访问器发生冲突。提示:在“Therapist.reviews”或“Therapist.appts”的定义中添加或更改related_name 参数。

标签: django many-to-many models


【解决方案1】:
class Therapist(models.Model):
    name = models.CharField(max_length=255)
    reviews = models.ManyToManyField(Client, related_name='therapist_reviews', through="Review")
    appts = models.ManyToManyField(Client, related_name='therapist_appts', through="Appointment")
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    objects = UserManager()

    def __repr__(self):
        return "<Book object: {}>".format(self.title)

如果你不为many_to_many字段分配related_name,默认的related会设置为model_lower_case_set,但是当你有两个many_to_many字段与同一个模型相关时,这两个字段的默认related_name与model_lower_case_set相同,这会在使用related_name查询queryset时产生冲突。

【讨论】:

  • 如果显式设置外键,为什么需要相关名称?以及如何将外键与相关名称结合使用?在这方面还是有点模糊。谢谢。
  • doc解释清楚
猜你喜欢
  • 1970-01-01
  • 2019-08-17
  • 1970-01-01
  • 2018-04-04
  • 1970-01-01
  • 1970-01-01
  • 2016-11-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多