【发布时间】:2023-03-31 22:50:01
【问题描述】:
from django.db import models
class Appointment(models.Model):
doctors = models.ManyToManyField('Doctor', through='AppointmentAccess', related_name='appointments')
class Doctor(models.Model):
appointments = models.ManyToManyField('Appointment', through='AppointmentAccess', related_name='doctors')
我收到以下错误:
core.Appointments.doctors: (fields.E302) Reverse accessor for 'PatientProfile.users' clashes with field name 'Doctor.appointments'.
HINT: Rename field 'Doctor.appointments', or add/change a related_name argument to the definition for field 'Appointment.doctors'.
core.Appointment.doctors: (fields.E303) Reverse query name for 'Doctor.appointments' clashes with field name 'Appointment.doctors'.
HINT: Rename field 'Doctor.appointments', or add/change a related_name argument to the definition for field 'Appointment.doctors'.
core.Doctor.appointments: (fields.E302) Reverse accessor for 'Doctor.appointments' clashes with field name 'Appointments.doctors'.
HINT: Rename field 'Appointment.doctors', or add/change a related_name argument to the definition for field 'Doctor.appointments'.
core.User.patient_profiles: (fields.E303) Reverse query name for 'Doctor.appointments' clashes with field name 'Appointments.doctors'.
HINT: Rename field 'Appointments.doctors', or add/change a related_name argument to the definition for field 'Doctor.appointments'.
为什么?有人可以帮我理解这个错误吗?应该通过 AppointmentAccess 表跟踪多对多字段,而不是生成新的中间表。相关姓名预约与医生有何冲突?
【问题讨论】:
-
为什么两个模型都需要指定m2m?只需在其中一个中指定即可。
标签: python django database django-models orm