我正在尝试使用django创建关注者系统,并遇到上述错误
代码
models.py
class Following(models.Model):
user=models.OneToOneField(User, on_delete=models.CASCADE)
users_ifollow=models.ManyToManyField(User, related_name='followed_by')
def __str__(self):
return f'{self.user.username} following'
views.py
class UserFollowView(View):
def get(self, request, username, *args, **kwargs):
toggle_user=get_object_or_404(User,username__iexact=username)
if request.user.is_authenticated:
user_profile, created=Following.objects.get_or_create(user=request.user)
if toggle_user in Following.objects.filter(user=request.user, users_ifollow=toggle_user):
Following.objects.remove(user=request.user, users_ifollow=toggle_user)
else:
Following.objects.create(user=request.user, users_ifollow=toggle_user)
return render(request, 'users/not_creator.html')
urls.py
path('user/<str:username>/follow', UserFollowView.as_view(),name='follow_user'),
回溯
跟踪:
文件 “ /home/Grayocean/.virtualenvs/myenv/lib/python3.7/site-packages/django/core/handlers/exception.py” 在内部 34. response = get_response(request)
文件 “ /home/Grayocean/.virtualenvs/myenv/lib/python3.7/site-packages/django/core/handlers/base.py” 在_get_response中 115. response = self.process_exception_by_middleware(e,request)
文件 “ /home/Grayocean/.virtualenvs/myenv/lib/python3.7/site-packages/django/core/handlers/base.py” 在_get_response中 113. response = wrapd_callback(request,* callback_args,** callback_kwargs)
文件 “ /home/Grayocean/.virtualenvs/myenv/lib/python3.7/site-packages/django/views/generic/base.py” 鉴于 71. return self.dispatch(request,* args,** kwargs)
文件 “ /home/Grayocean/.virtualenvs/myenv/lib/python3.7/site-packages/django/views/generic/base.py” 在派遣中 97.返回处理程序(请求,* args,** kwargs)
获取文件“ /home/Grayocean/grayocean.co/blog/views.py” 74. following.objects.create(user = request.user,users_ifollow = toggle_user)
文件 “ /home/Grayocean/.virtualenvs/myenv/lib/python3.7/site-packages/django/db/models/manager.py” 在manager_method中 82. return getattr(self.get_queryset(),name)(* args,** kwargs)
文件 “ /home/Grayocean/.virtualenvs/myenv/lib/python3.7/site-packages/django/db/models/query.py” 在创造 420. obj = self.model(** kwargs)
文件 “ /home/Grayocean/.virtualenvs/myenv/lib/python3.7/site-packages/django/db/models/base.py” 在初始化中 496. _setattr(self,prop,kwargs [prop])
文件 “ /home/Grayocean/.virtualenvs/myenv/lib/python3.7/site-packages/django/db/models/fields/related_descriptors.py” 在设置中 538.%self._get_set_deprecation_msg_params(),
异常类型:/ user / rheazes /中的TypeError遵循异常值: 直接分配给多对多集的正面是 禁止。改用users_ifollow.set()。