Django 中的一对一关系
One To One relationship in Django
我需要为每个用户创建一个配置文件,包括地址和城市等等...在 Django 模型中使用 OneToOne 关系,所以我不知道该怎么做,我想继承 class 来自 Class 用户的个人资料。
我遇到了这个错误:
django.core.exceptions.FieldError: Unknown field(s) (address) specified for User
这是我的模型:
#models.py
from django.db import models
from django.contrib.auth.models import (
BaseUserManager, AbstractBaseUser
)
from phonenumber_field.modelfields import PhoneNumberField
class UserManager(BaseUserManager):
def create_user(self, email, fav_color, lastname, password=None, is_active=True, is_staff=False, is_admin=False,):
"""
Creates and saves a User with the given email and password.
"""
if not email:
raise ValueError('Users must have an email address')
# if not phone_number:
# raise ValueError('user must have phone number')
user = self.model(
email=self.normalize_email(email),
fav_color=fav_color,
lastname=lastname,
city=profile.city,
address=profile.address,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_staffuser(self, email, password):
"""
Creates and saves a staff user with the given email and password.
"""
user = self.create_user(
email,
password=password,
)
user.staff = True
user.save(using=self._db)
return user
def create_superuser(self, email, password, fav_color, lastname,):
"""
Creates and saves a superuser with the given email and password.
"""
user = self.create_user(
email,
password=password,
fav_color=fav_color,
lastname=lastname,
)
user.staff = True
user.admin = True
user.save(using=self._db)
return user
class User(AbstractBaseUser):
email = models.EmailField(
verbose_name='email address', max_length=255, unique=True)
lastname = models.CharField(max_length=100)
fav_color = models.CharField(max_length=10)
active = models.BooleanField(default=True)
staff = models.BooleanField(default=False) # a admin user; non super-user
admin = models.BooleanField(default=False) # a superuser
# notice the absence of a "Password field", that is built in.
objects = UserManager()
USERNAME_FIELD = 'email'
# Email & Password are required by default.
REQUIRED_FIELDS = ['fav_color', 'lastname',]
def get_full_name(self):
# The user is identified by their email address
return self.email
def get_short_name(self):
# The user is identified by their email address
return self.email
def __str__(self): # __unicode__ on Python 2
return self.email
def has_perm(self, perm, obj=None):
"Does the user have specific permission?"
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"Does the user have permission to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True
@property
def is_staff(self):
"Is the user a member of staff?"
return self.staff
@property
def is_admin(self):
"Is the user an admin member?"
return self.admin
@property
def is_active(self):
"Is the user active?"
return self.active
class profile(User):
user = models.OneToOneField(User, on_delete=models.CASCADE),
address = models.CharField(max_length=255),
city = models.CharField(max_length=120),
实际上我不知道如何使用 OneToOne 模型,以及如何从基础 class 继承以订购 classes
请帮忙,坦克你。
你不能那样使用 Django 模型。 User(AbstractBaseUser)
是正确的,因为 AbstractBaseUser
是抽象的。有2个选项
- 不推荐,为用户添加更多字段(城市、地址...)。
- 像您一样定义
class Profile(models.Model)
,创建用户后创建配置文件。
怎么办?
假设您有一个包含 Profile
模型的 accounts
应用程序。
文件accounts/models.py,像你一样定义配置文件字段
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
address = models.CharField(max_length=255)
city = models.CharField(max_length=120)
文件accounts/__init__.py
,定义应用程序配置将被使用。
default_app_config = "accounts.apps.Config"
文件accounts/apps.py
from django.apps import AppConfig
class Config(AppConfig):
name = 'accounts'
def ready(self):
from . import signals
文件accounts/signals.py,安装信号
from django.conf import settings
from django.db.models.signals import post_save
from django.dispatch import receiver, Signal
from accounts.models import Profile
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_related_handler(sender, instance, created, **kwargs):
"""
Once a new User instance was saved:
Check User instance, if this is new instance (created is True)
then create a Profile for this user.
"""
if not created:
return
default_data = dict(city='', address='')
instance.profile = Profile.objects.create(user=instance, **default_data)
不要忘记将 accounts
添加到您的 INSTALLED_APPS 配置中。
我需要为每个用户创建一个配置文件,包括地址和城市等等...在 Django 模型中使用 OneToOne 关系,所以我不知道该怎么做,我想继承 class 来自 Class 用户的个人资料。
我遇到了这个错误:
django.core.exceptions.FieldError: Unknown field(s) (address) specified for User
这是我的模型: #models.py
from django.db import models
from django.contrib.auth.models import (
BaseUserManager, AbstractBaseUser
)
from phonenumber_field.modelfields import PhoneNumberField
class UserManager(BaseUserManager):
def create_user(self, email, fav_color, lastname, password=None, is_active=True, is_staff=False, is_admin=False,):
"""
Creates and saves a User with the given email and password.
"""
if not email:
raise ValueError('Users must have an email address')
# if not phone_number:
# raise ValueError('user must have phone number')
user = self.model(
email=self.normalize_email(email),
fav_color=fav_color,
lastname=lastname,
city=profile.city,
address=profile.address,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_staffuser(self, email, password):
"""
Creates and saves a staff user with the given email and password.
"""
user = self.create_user(
email,
password=password,
)
user.staff = True
user.save(using=self._db)
return user
def create_superuser(self, email, password, fav_color, lastname,):
"""
Creates and saves a superuser with the given email and password.
"""
user = self.create_user(
email,
password=password,
fav_color=fav_color,
lastname=lastname,
)
user.staff = True
user.admin = True
user.save(using=self._db)
return user
class User(AbstractBaseUser):
email = models.EmailField(
verbose_name='email address', max_length=255, unique=True)
lastname = models.CharField(max_length=100)
fav_color = models.CharField(max_length=10)
active = models.BooleanField(default=True)
staff = models.BooleanField(default=False) # a admin user; non super-user
admin = models.BooleanField(default=False) # a superuser
# notice the absence of a "Password field", that is built in.
objects = UserManager()
USERNAME_FIELD = 'email'
# Email & Password are required by default.
REQUIRED_FIELDS = ['fav_color', 'lastname',]
def get_full_name(self):
# The user is identified by their email address
return self.email
def get_short_name(self):
# The user is identified by their email address
return self.email
def __str__(self): # __unicode__ on Python 2
return self.email
def has_perm(self, perm, obj=None):
"Does the user have specific permission?"
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"Does the user have permission to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True
@property
def is_staff(self):
"Is the user a member of staff?"
return self.staff
@property
def is_admin(self):
"Is the user an admin member?"
return self.admin
@property
def is_active(self):
"Is the user active?"
return self.active
class profile(User):
user = models.OneToOneField(User, on_delete=models.CASCADE),
address = models.CharField(max_length=255),
city = models.CharField(max_length=120),
实际上我不知道如何使用 OneToOne 模型,以及如何从基础 class 继承以订购 classes 请帮忙,坦克你。
你不能那样使用 Django 模型。 User(AbstractBaseUser)
是正确的,因为 AbstractBaseUser
是抽象的。有2个选项
- 不推荐,为用户添加更多字段(城市、地址...)。
- 像您一样定义
class Profile(models.Model)
,创建用户后创建配置文件。
怎么办?
假设您有一个包含 Profile
模型的 accounts
应用程序。
文件accounts/models.py,像你一样定义配置文件字段
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
address = models.CharField(max_length=255)
city = models.CharField(max_length=120)
文件accounts/__init__.py
,定义应用程序配置将被使用。
default_app_config = "accounts.apps.Config"
文件accounts/apps.py
from django.apps import AppConfig
class Config(AppConfig):
name = 'accounts'
def ready(self):
from . import signals
文件accounts/signals.py,安装信号
from django.conf import settings
from django.db.models.signals import post_save
from django.dispatch import receiver, Signal
from accounts.models import Profile
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_related_handler(sender, instance, created, **kwargs):
"""
Once a new User instance was saved:
Check User instance, if this is new instance (created is True)
then create a Profile for this user.
"""
if not created:
return
default_data = dict(city='', address='')
instance.profile = Profile.objects.create(user=instance, **default_data)
不要忘记将 accounts
添加到您的 INSTALLED_APPS 配置中。