我有两个类,它们之间有两个与许多关系。由于相邻的表也必须具有其他属性,因此我明确定义了它们。但是我遇到了一个错误,说我需要更改相关名称。不确定在哪里做。任何帮助都非常感谢。
这是我的型号。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)
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
字段分配RESSID_NAME,则将默认相关的相关设置为model_lower_case_set,但是当您具有两个与同一模型相关的many_to_many
字段时,两个归档的默认值相关_name与model_lower_case_set相同使用related_name
查询QuerySet时的冲突。