作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建的另一个网站没有这个问题,据我所知,它的设置方式相同。
这是我来自用户应用程序的 models.py:
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from users.choices import *
from django.db.models.signals import post_save
from django.dispatch import receiver
from listings.models import University
# Create your models here.
class SCUser(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
first_name = models.CharField(max_length=50,blank=False,default='User')
join_date = models.DateTimeField(default=timezone.now)
university = models.ForeignKey(University,related_name='u_university',null=False,blank=False)
def __str__(self):
return self.user.username
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
SCUser.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.scuser.save()
这里是错误:
RelatedObjectDoesNotExist at /admin/login/
User has no scuser.
这是回溯:
Internal Server Error: /admin/login/
Traceback (most recent call last):
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/contrib/admin/sites.py", line 393, in login
return LoginView.as_view(**defaults)(request)
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/utils/decorators.py", line 67, in _wrapper
return bound_func(*args, **kwargs)
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/views/decorators/debug.py", line 76, in sensitive_post_parameters_wrapper
return view(request, *args, **kwargs)
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/utils/decorators.py", line 63, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/utils/decorators.py", line 67, in _wrapper
return bound_func(*args, **kwargs)
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/utils/decorators.py", line 63, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/utils/decorators.py", line 67, in _wrapper
return bound_func(*args, **kwargs)
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/utils/decorators.py", line 63, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/contrib/auth/views.py", line 90, in dispatch
return super(LoginView, self).dispatch(request, *args, **kwargs)
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/views/generic/edit.py", line 183, in post
return self.form_valid(form)
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/contrib/auth/views.py", line 119, in form_valid
auth_login(self.request, form.get_user())
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/contrib/auth/__init__.py", line 160, in login
user_logged_in.send(sender=user.__class__, request=request, user=user)
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/dispatch/dispatcher.py", line 193, in send
for receiver in self._live_receivers(sender)
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/dispatch/dispatcher.py", line 193, in <listcomp>
for receiver in self._live_receivers(sender)
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/contrib/auth/models.py", line 25, in update_last_login
user.save(update_fields=['last_login'])
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/contrib/auth/base_user.py", line 80, in save
super(AbstractBaseUser, self).save(*args, **kwargs)
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/db/models/base.py", line 807, in save
force_update=force_update, update_fields=update_fields)
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/db/models/base.py", line 847, in save_base
update_fields=update_fields, raw=raw, using=using,
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/dispatch/dispatcher.py", line 193, in send
for receiver in self._live_receivers(sender)
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/dispatch/dispatcher.py", line 193, in <listcomp>
for receiver in self._live_receivers(sender)
File "/Users/garrettlove/Documents/Student-Cribz/Website/newsite/users/models.py", line 29, in save_user_profile
instance.scuser.save()
File "/anaconda3/envs/dev/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 407, in __get__
self.related.get_accessor_name()
django.db.models.fields.related_descriptors.RelatedObjectDoesNotExist: User has no scuser.
我环顾四周,似乎找不到与此相关的任何内容,因为我已经在创建新用户时创建了适当的实例。如果您需要查看项目的任何其他部分,请告诉我。
最佳答案
听起来您的应用中已有用户。最简单的选择是使用 get_or_create()
更新这些用户。
for user in User.objects.all():
UserProfile.objects.get_or_create(user=user)
您可以在 shell 中执行,在迁移中运行或删除数据库(如果在新开发中)并从那里开始。
关于python - RelatedObjectDoesNotExist at/admin/login/User 没有 scuser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48159350/
我创建的另一个网站没有这个问题,据我所知,它的设置方式相同。 这是我来自用户应用程序的 models.py: from django.db import models from django.util
我是一名优秀的程序员,十分优秀!