Django 如何在用户模型中添加用户外键?
Django How to add a User Foreignkey inside a User model?
我试图在用户模型中插入用户的外键,但我一直收到错误。
作为用户,我想有两个目的,我可以做一本书来照顾我的宠物,也可以照顾其他宠物
class User(AbstractBaseUser, PermissionsMixin):
name = models.CharField(max_length=255, default='John Doe')
pet_sitter = models.ForeignKey('User',on_delete=models.CASCADE)//--->User inside User
I tried get_user_model() it showed errors too//
pet_appointments= models.ManyToManyField(
'User',
through=Booking,
through_fields=('patient', 'doctor')
)
class Booking(models.Model):
start_date = models.DateTimeField()
end_date = models.DateTimeField()
owner_of_pet = models.ForeignKey(get_user_model(),related_name = "User",on_delete=models.CASCADE, blank=True,null=True)
sitter = models.ForeignKey('User', on_delete=models.CASCADE)
enter code here
===============
error
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'api.User' that has not been installed
- 似乎您在
api
应用程序中定义了一些 User
,因此将该应用程序添加到 settings.py
中已安装的应用程序中:
# settings.py
INSTALLED_APPS = [
...
"api",
此外,实际上不可能也不需要以这种方式引用 User
。
我想象你的 models.py
是这样的:
from api.models import CustomUser
from django.db import models
class Pet(models.Model):
name = models.CharField(max_length=100)
pet_sitter = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
owner = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='pets', blank=True,null=True)
class Booking(models.Model):
pet = models.ForeignKey(Pet, on_delete=models.CASCADE)
start_date = models.DateTimeField()
end_date = models.DateTimeField()
在这里查看源代码:https://github.com/almazkun/petshop
我试图在用户模型中插入用户的外键,但我一直收到错误。 作为用户,我想有两个目的,我可以做一本书来照顾我的宠物,也可以照顾其他宠物
class User(AbstractBaseUser, PermissionsMixin):
name = models.CharField(max_length=255, default='John Doe')
pet_sitter = models.ForeignKey('User',on_delete=models.CASCADE)//--->User inside User
I tried get_user_model() it showed errors too//
pet_appointments= models.ManyToManyField(
'User',
through=Booking,
through_fields=('patient', 'doctor')
)
class Booking(models.Model):
start_date = models.DateTimeField()
end_date = models.DateTimeField()
owner_of_pet = models.ForeignKey(get_user_model(),related_name = "User",on_delete=models.CASCADE, blank=True,null=True)
sitter = models.ForeignKey('User', on_delete=models.CASCADE)
enter code here
===============
error
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'api.User' that has not been installed
- 似乎您在
api
应用程序中定义了一些User
,因此将该应用程序添加到settings.py
中已安装的应用程序中:
# settings.py
INSTALLED_APPS = [
...
"api",
此外,实际上不可能也不需要以这种方式引用 User
。
我想象你的 models.py
是这样的:
from api.models import CustomUser
from django.db import models
class Pet(models.Model):
name = models.CharField(max_length=100)
pet_sitter = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
owner = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='pets', blank=True,null=True)
class Booking(models.Model):
pet = models.ForeignKey(Pet, on_delete=models.CASCADE)
start_date = models.DateTimeField()
end_date = models.DateTimeField()
在这里查看源代码:https://github.com/almazkun/petshop