我正在尝试创建一个自定义注册表单,我已经扩展了 django 内置用户模型。我还使用 django-postman 为用户提供用户消息.Postman在我使用内置用户模型之前工作得非常好,但是当我使用自定义用户模型时,它开始向我显示此查找错误(仅在创建消息时)。我已经做了一些研究来解决这个问题但是失败了。
追溯错误
class MadeLookupChannel(LookupChannel):
File "C:\Users\vishw\Envs\Vishwesh2_env\lib\site-pack
ages\ajax_select\registry.py", line 104, in MadeLookupC
hannel
model = get_model(app_label, model_name)
File "C:\Users\vishw\Envs\Vishwesh2_env\lib\site-pack
ages\ajax_select\registry.py", line 123, in get_model
return apps.get_model(app_label, model_name)
File "C:\Users\vishw\Envs\Vishwesh2_env\lib\site-pack
ages\django\apps\registry.py", line 205, in get_model
return app_config.get_model(model_name, require_rea
dy=require_ready)
File "C:\Users\vishw\Envs\Vishwesh2_env\lib\site-pack
ages\django\apps\config.py", line 172, in get_model
"App '%s' doesn't have a '%s' model." % (self.label, model_name))
LookupError: App 'accounts' doesn't have a 'user' model
但是我还在settings.py文件中添加了这个来解决这个问题
AUTH_USER_MODEL = 'accounts.Profile'
然后显示此错误
File "C:\Users\vishw\OneDrive\Documents\Projects_2\si
mple_social_clone\simplesocial\postman\apps.py", line 1
3, in ready
setup()
File "C:\Users\vishw\OneDrive\Documents\Projects_2\si
mple_social_clone\simplesocial\postman\models.py", line
57, in setup
name_user_as = getattr(settings, 'POSTMAN_NAME_USER
_AS', get_user_model().USERNAME_FIELD)
AttributeError: type object 'Profile' has no attribute
'USERNAME_FIELD'
这是我的accounts.models.py这里我的Profile模型扩展了内置的用户模型
from django.contrib.auth.models import User
from django.db import models
from django.contrib import auth
from django.utils import timezone
from django.dispatch import receiver
from django.db.models.signals import post_save
class Profile(models.Model):
GENDER_CHOICES=(
('M','Male'),
('F','Female'),
('O','Other')
)
BRANCH_CHOICES=(
('IT','Information Technology'),
('COMP','Computer Science')
)
QUAL_CHOICES=(
('BE','BE-Bachelor of Engineering'),
('Btech','Btech-Bachelor of Technology'),
('ME','ME-Master of Engineering'),
('Mtech','Mtech-Master of Technology')
)
user=models.OneToOneField(User,on_delete=models.CASCADE)
sex=models.CharField(max_length=5,choices=GENDER_CHOICES)
branch=models.CharField(max_length=5,choices=BRANCH_CHOICES)
mob=models.CharField(max_length=10)
highest_qual=models.CharField(max_length=15,choices=QUAL_CHOICES)
def __str__(self):
return self.user.username
@receiver(post_save,sender=User)
def update_user_profile(sender,instance,created,**kwargs):
if created:
Profile.objects.create(user=instance)
instance.profile.save()
这是发生错误的postman.models.py文件snap
def setup():
"""
Deferred actions, that can not be done at import time since Django 1.7.
Normally called in AppConfig.ready().
For backwards compatibility, also called on first need.
"""
from django.contrib.auth import get_user_model
name_user_as = getattr(settings, 'POSTMAN_NAME_USER_AS', get_user_model().USERNAME_FIELD)
ORDER_BY_FIELDS.update({
'f': 'sender__' + name_user_as, # as 'from'
't': 'recipient__' + name_user_as, # as 'to'
's': 'subject', # as 'subject'
'd': 'sent_at', # as 'date'
})
请帮助我!
答案 0 :(得分:1)
您没有拥有自定义用户模型。您有一个链接到内置用户模型的Profile模型。无需设置AUTH_USER_MODEL;你应该删除那个设置。