我还有另一个网站,它没有这个问题,并且据我所知设置的方式相同。
以下是用户应用程序的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.
我已经找了一圈,似乎找不到任何与此相关的信息,因为我已经在创建新用户时创建了适当的实例。如果您需要查看项目的其他部分,请告诉我。
python
然后输入那两行代码或其他什么吗? - Garrett