Reputation: 1
I have two classes that have two many to many relationships between them. Since the adjoining tables also have to have additional attributes, I explicitly defined them. But I'm getting an error that says I need to change the related names. Not sure where to do this. Any help is greatly appreciated.
Here's my models.py code:
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)
Upvotes: 0
Views: 1091
Reputation: 7717
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)
If you don't assign related_name for many_to_many
field,the default related will set to model_lower_case_set,but when you has two many_to_many
fields related to one same model,the default related_name for the two fileds is same as model_lower_case_set,this will cause conflict when use related_name
to query queryset.
Upvotes: 2