gpt4 book ai didi

python - Django 1.7 - 为定义添加或更改 related_name 参数

转载 作者:行者123 更新时间:2023-11-28 19:48:52 25 4
gpt4 key购买 nike

你好,我正在尝试制作 Django 自定义用户模型,但出现这样的错误

    auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'UserProfile.groups'.
HINT: Add or change a related_name argument to the definition for 'User.groups' or 'UserProfile.groups'.
auth.User.user_permissions: (fields.E304) Reverse accessor for 'User.user_permissions' clashes with reverse accessor for 'UserProfile.user_permissions'.
HINT: Add or change a related_name argument to the definition for 'User.user_permissions' or 'UserProfile.user_permissions'.
kerajinan.Product.author: (fields.E300) Field defines a relation with model 'settings.AUTH_USER_MODEL', which is either not installed, or is abstract.
kerajinan.UserProfile.groups: (fields.E304) Reverse accessor for 'UserProfile.groups' clashes with reverse accessor for 'User.groups'.
HINT: Add or change a related_name argument to the definition for 'UserProfile.groups' or 'User.groups'.
kerajinan.UserProfile.user_permissions: (fields.E304) Reverse accessor for 'UserProfile.user_permissions' clashes with reverse accessor for 'User.user_permissions'.
HINT: Add or change a related_name argument to the definition for 'UserProfile.user_permissions' or 'User.user_permissions'.

我使用 django 1.7 这是 setting.py 的一部分,我的名字应用程序是 'kerajinan:

AUTH_USER_MODEl = 'kerajinan.UserProfile'

模型.py:

class Product(models.Model):
author = models.ForeignKey('settings.AUTH_USER_MODEL')
userprofile = models.ForeignKey('UserProfile')
title = models.CharField(max_length=50)

def __str__(self):
return self.title;
class MyUserManager(BaseUserManager):
def create_user(self, username, email, password=None):
if not email:
raise ValueError('Users must have an email address')
if not username:
raise ValueError('Users must have a username')

user = self.model(
username = username,
email = self.normalize_email(email),
)
user.is_active = True
user.set_password(password)
user.save(using=self._db)
return user

def create_superuser(self, username, email, password):
user = self.create_user(username=username,email=email, password=password)

user.is_staff = True
user.is_superuser = True
user.save(using=self._db)
return user

class UserProfile(AbstractBaseUser, PermissionsMixin):

alphanumeric = RegexValidator(r'^[0-9a-zA-Z]*$', message='Only alphanumeric characters are allowed.')
username = models.CharField(unique=True, max_length=20, validators=[alphanumeric])
email = models.EmailField(verbose_name='email address', unique=True, max_length=244)
first_name = models.CharField(max_length=30, null=True, blank=True)
last_name = models.CharField(max_length=50, null=True, blank=True)
is_active = models.BooleanField(default=True, null=False)
is_staff = models.BooleanField(default=False, null=False)


# profile_image = models.ImageField(upload_to="uploads", blank=False, null=False, default="/static/images/defaultuserimage.jpg")

objects = MyUserManager()

USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']

def get_full_name(self):
fullname = self.first_name+" "+self.last_name
return self.fullname

def get_short_name(self):
return self.username

def __str__(self):
return self.email

我在设置的类产品部分遇到错误。AUTH_USER_MODEL 这是错误:

  ERRORS:
kerajinan.Product.author: (fields.E300) Field defines a relation with model 'settings.AUTH_USER_MODEL', which is either not installed, or is abstract.

你能帮我解决这个错误吗?

最佳答案

您在设置名称中使用小写的 l。 Python 变量区分大小写,因此将其更改为大写 L:

AUTH_USER_MODEL = 'kerajinan.UserProfile'

更新:要创建与自定义用户模型的关系,您应该删除 FK 定义中的引号:

author = models.ForeignKey(settings.AUTH_USER_MODEL)

关于python - Django 1.7 - 为定义添加或更改 related_name 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29691116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com