当我尝试运行makemigrations时出现以下错误。
ERRORS:
remoshin_manager_sys.RemoshinDoctor.groups: (fields.E304) Reverse accessor for 'RemoshinDoctor.groups' clashes with reverse accessor for 'RemoshinManager.groups'.
HINT: Add or change a related_name argument to the definition for 'RemoshinDoctor.groups' or 'RemoshinManager.groups'.
remoshin_manager_sys.RemoshinDoctor.groups: (fields.E304) Reverse accessor for 'RemoshinDoctor.groups' clashes with reverse accessor for 'RemoshinUser.groups'.
HINT: Add or change a related_name argument to the definition for 'RemoshinDoctor.groups' or 'RemoshinUser.groups'.
remoshin_manager_sys.RemoshinDoctor.user_permissions: (fields.E304) Reverse accessor for 'RemoshinDoctor.user_permissions' clashes with reverse accessor for 'RemoshinManager.user_permissions'.
HINT: Add or change a related_name argument to the definition for 'RemoshinDoctor.user_permissions' or 'RemoshinManager.user_permissions'.
remoshin_manager_sys.RemoshinDoctor.user_permissions: (fields.E304) Reverse accessor for 'RemoshinDoctor.user_permissions' clashes with reverse accessor for 'RemoshinUser.user_permissions'.
HINT: Add or change a related_name argument to the definition for 'RemoshinDoctor.user_permissions' or 'RemoshinUser.user_permissions'.
remoshin_manager_sys.RemoshinManager.groups: (fields.E304) Reverse accessor for 'RemoshinManager.groups' clashes with reverse accessor for 'RemoshinDoctor.groups'.
HINT: Add or change a related_name argument to the definition for 'RemoshinManager.groups' or 'RemoshinDoctor.groups'.
remoshin_manager_sys.RemoshinManager.groups: (fields.E304) Reverse accessor for 'RemoshinManager.groups' clashes with reverse accessor for 'RemoshinUser.groups'.
HINT: Add or change a related_name argument to the definition for 'RemoshinManager.groups' or 'RemoshinUser.groups'.
remoshin_manager_sys.RemoshinManager.user_permissions: (fields.E304) Reverse accessor for 'RemoshinManager.user_permissions' clashes with reverse accessor for 'RemoshinDoctor.user_permissions'.
HINT: Add or change a related_name argument to the definition for 'RemoshinManager.user_permissions' or 'RemoshinDoctor.user_permissions'.
remoshin_manager_sys.RemoshinManager.user_permissions: (fields.E304) Reverse accessor for 'RemoshinManager.user_permissions' clashes with reverse accessor for 'RemoshinUser.user_permissions'.
HINT: Add or change a related_name argument to the definition for 'RemoshinManager.user_permissions' or 'RemoshinUser.user_permissions'.
remoshin_manager_sys.RemoshinUser.groups: (fields.E304) Reverse accessor for 'RemoshinUser.groups' clashes with reverse accessor for 'RemoshinDoctor.groups'.
HINT: Add or change a related_name argument to the definition for 'RemoshinUser.groups' or 'RemoshinDoctor.groups'.
remoshin_manager_sys.RemoshinUser.groups: (fields.E304) Reverse accessor for 'RemoshinUser.groups' clashes with reverse accessor for 'RemoshinManager.groups'.
HINT: Add or change a related_name argument to the definition for 'RemoshinUser.groups' or 'RemoshinManager.groups'.
remoshin_manager_sys.RemoshinUser.user_permissions: (fields.E304) Reverse accessor for 'RemoshinUser.user_permissions' clashes with reverse accessor for 'RemoshinDoctor.user_permissions'.
HINT: Add or change a related_name argument to the definition for 'RemoshinUser.user_permissions' or 'RemoshinDoctor.user_permissions'.
remoshin_manager_sys.RemoshinUser.user_permissions: (fields.E304) Reverse accessor for 'RemoshinUser.user_permissions' clashes with reverse accessor for 'RemoshinManager.user_permissions'.
HINT: Add or change a related_name argument to the definition for 'RemoshinUser.user_permissions' or 'RemoshinManager.user_permissions'.
我可以获得有关此错误的大量信息。但似乎在我的情况下没有。
我有3个项目,每个项目都有不同的自定义用户模型。我使用了3个项目中的一个数据库,每个项目都有自己的自定义用户模型。
remoshin_user_sys / RemoshinUser
remoshin_clinic_sys / RemoshinDoctor
remoshin_manager_sys / RemoshinManager
当我尝试从remoshin_manager_sys运行makemigrations时发生此错误。我不想将RemoshinDoctor和RemoshinUser设置为RemoshinManager的自定义用户模型。
我的settings.py如下。
AUTH_USER_MODEL = 'remoshin_manager_sys.RemoshinManager'
如何避免此错误?
添加我的models.py。
class RemoshinUserManager(BaseUserManager):
def _create_user(self, email, password, is_staff, is_superuser, **extra_fields):
now = timezone.now()
if not email:
raise ValueError('User must have a e-mail address.')
email = self.normalize_email(email)
user = self.model(email=email,
is_staff=is_staff, is_active=True,
is_superuser=is_superuser, last_login=now,
date_joined=now, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, password, **extra_fields):
return self.create_user(email, password, False, False, **extra_fields)
def create_superuser(self, email, password):
return self.create_user(user_email, password, True, True, **extra_fields)
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_auth_token(sender, instance=None, created=False, **kwargs):
if created:
Token.objects.create(user=instance)
class RemoshinUser(AbstractBaseUser, PermissionsMixin):
email = models.CharField(max_length=128, primary_key=True)
username = models.CharField(max_length=64)
user_kana_name = models.CharField(max_length=32, blank=True)
password = models.CharField(max_length=128)
user_birthdate = models.DateField(null=True, blank=True)
user_sex = models.SmallIntegerField(null=True)
user_postno = models.CharField(max_length=7, blank=True)
user_address1 = models.CharField(max_length=128)
user_address2 = models.CharField(max_length=128, blank=True)
user_telno = models.CharField(max_length=16, blank=True)
user_photo = models.ImageField(blank=True)
authentication_status = models.BooleanField(default=False)
create_date = models.DateTimeField(auto_now_add=True)
modify_date = models.DateTimeField(auto_now=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['user_name', 'user_telno']
objects = RemoshinUserManager()
class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')
db_table = 'remosys_remoshin_user_tbl'
def get_absolute_url(self):
return "/users/%s" % urlquote(self.email)
def email_user(self, subject, message, from_email=None):
send_mail(subject, message, from_email, [self_email])
class RemoshinDoctorManager(BaseUserManager):
def _create_user(self, email, password, is_staff, is_superuser, **extra_fields):
now = timezone.now()
if not email:
raise ValueError('Doctor must have a e-mail address.')
email = self.normalize_email(email)
doctor = self.model(email=email,
is_staff=is_staff, is_active=True,
is_superuser=is_superuser, last_login=now,
date_joined=now, **extra_fields)
doctor.set_password(password)
doctor.save(using=self._db)
return doctor
def create_user(self, email, password, **extra_fields):
return self.create_user(email, password, False, False, **extra_fields)
def create_superuser(self, email, password):
return self.create_user(email, password, True, True, **extra_fields)
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_auth_token(sender, instance=None, created=False, **kwargs):
if created:
Token.objects.create(user=instance)
class RemoshinDoctor(AbstractBaseUser, PermissionsMixin):
doctor_id = models.CharField(max_length=16, primary_key=True)
clinic_id = models.ForeignKey(Clinic)
email = models.CharField(max_length=128)
password = models.CharField(max_length=128)
username = username = models.CharField(max_length=64)
doctor_photo = models.ImageField(blank=True, null=True)
create_date = models.DateTimeField(auto_now_add=True)
modify_date = models.DateTimeField(auto_now=True)
USERNAME_FIELD = 'doctor_id'
REQUIRED_FIELDS = ['clinic_id', 'email', 'password', 'username']
objects = RemoshinDoctorManager()
class Meta:
verbose_name = _('doctor')
verbose_name_plural = _('doctors')
db_table = 'remosys_remoshin_doctor_tbl'
def get_absolute_url(self):
return "/doctors/%s" % urlquote(self.doctor_id)
class RemoshinManagerManager(BaseUserManager):
def _create_user(self, username, email, password, is_staff, is_admin, **extra_fields):
now = timezone.now()
if not email:
raise ValueError('Manager must have a e-mail address.')
email = self.normalize_email(email)
manager = self.model(username=username, email=email,
is_active=True,
is_superuser=is_admin,
is_staff=is_staff,
create_date=now,
modify_date=now, **extra_fields)
manager.set_password(password)
manager.save(using=self._dåb)
return manager
def create_user(self, username, email, password, **extra_fields):
return self._create_user(username, email, password, False, False, **extra_fields)
def create_superuser(self, username, email, password, **extra_fields):
return self._create_user(username, email, password, True, True, **extra_fields)
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_auth_token(sender, instance=None, created=False, **kwargs):
if created:
Token.objects.create(user=instance)
class RemoshinManager(AbstractBaseUser, PermissionsMixin):
manager_id = models.CharField(max_length=16, primary_key=True)
email = models.CharField(max_length=128)
password = models.CharField(max_length=128)
username = models.CharField(max_length=64)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
create_date = models.DateTimeField(default=timezone.now)
modify_date = models.DateTimeField(default=timezone.now)
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
objects = RemoshinManagerManager()
class Meta:
verbose_name = _('manager')
verbose_name_plural = _('managers')
db_table = 'remosys_remoshin_manager_tbl'
def get_absolute_url(self):
return "/managers/%s" % urlquote(self.manager_id)
def email_user(self, subject, message, from_email=None):
send_mail(subject, message, from_email, [self.email])
def user_has_perm(username, perm, obj):
A backend can raise `PermissionDenied` to short-circuit permission checking.
return _user_has_perm(username, perm, obj)
def has_perm(self, perm, obj=None):
return _user_has_perm(self, perm, obj=obj)
def has_module_perms(self, app_label):
return self.is_superuser
def get_short_name(self):
return self.first_name
@property
def is_superuser(self):
return self.is_admin
class Consultation(models.Model):
consultation_id = models.CharField(max_length=8, primary_key=True)
user_email = models.ForeignKey(RemoshinUser)
clinic_id = models.ForeignKey(Clinic)
doctor_id = models.ForeignKey(RemoshinDoctor)
consultation_datetime = models.DateTimeField(auto_now=True)
consultation_status = models.BooleanField(default=False)
prescription = models.TextField(blank=True)
prescription_status = models.SmallIntegerField(default=0)
payment_status = models.SmallIntegerField(default=0)
create_date = models.DateTimeField(auto_now_add=True)
modify_date = models.DateTimeField(auto_now=True)
class Meta:
db_table = 'remosys_consultation_tbl'
# swappable = 'settings.AUTH_USER_MODEL'
答案 0 :(得分:2)
我将用一个例子解释错误:
请说明你有这两种模式。
class Book(Models.model):
title = models.CharField(maxlength = 100)
class Person(Models.model):
name = models.CharField(max_length = 100)
own_book = models.ForeignKey('Book')
简单模型,但棘手的部分是Django在定义外键时构建反向引用:如果person
是查询对象,那么对她拥有的书名称的引用将是:{{1 }}。
现在,如果你想知道拥有某本书的人名? Django构建了反向关系,默认情况下采用模型名称。也就是说,' book'作为查询集对象,您可以使用person.own_book.title
找到拥有该书的人员的姓名。很酷,对吗?它通过你的外键查找。
现在,将另一个外键添加到同一本书模型中:
book.person.name
您的前瞻性关系仍然定义明确:class Person(Models.model):
name = models.CharField(max_length = 100)
wrote_book = models.ForeignKey('Book')
own_book = models.ForeignKey('Book')
和person.wrote_book.title
。但person.own_book.title
需要做什么?寻找拥有这本书的人的姓名?谁写过这本书?或两者? django会抛出你的book.person.name
。
如何解决? Djangos告诉您在模型中创建一个related_name:
Reverse acessor error
因此class Person(Models.model):
name = models.CharField(max_length = 100)
wrote_book = models.ForeignKey('Book', related_name = 'wrote')
own_book = models.ForeignKey('Book', related_name = 'own')
不再存在(默认会被覆盖),并被book.person.name
和book.own.name
取代。
所以我的猜测是:查找book.wrote.name
和groups
模型中定义的外键,并在其上放置related_names参数。