gpt4 book ai didi

python - Django:禁止直接赋值给相关集合的反面。使用 username.set() 代替

转载 作者:太空狗 更新时间:2023-10-30 01:18:15 24 4
gpt4 key购买 nike

我在 django 中迁移我的数据库时遇到了一些麻烦

Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "E:\Env\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "E:\Env\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "E:\Env\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "E:\Env\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 350, in execute
self.check()
File "E:\Env\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 379, in check
include_deployment_checks=include_deployment_checks,
File "E:\Env\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 366, in _run_checks
return checks.run_checks(**kwargs)
File "E:\Env\Python\Python37-32\lib\site-packages\django\core\checks\registry.py", line 71, in run_checks
new_errors = check(app_configs=app_configs)
File "E:\Env\Python\Python37-32\lib\site-packages\django\contrib\auth\checks.py", line 74, in check_user_model
if isinstance(cls().is_anonymous, MethodType):
File "E:\Env\Python\Python37-32\lib\site-packages\django\db\models\base.py", line 470, in __init__
_setattr(self, field.attname, val)
File "E:\Env\Python\Python37-32\lib\site-packages\django\db\models\fields\related_descriptors.py", line 537, in __set__
% self._get_set_deprecation_msg_params(),
TypeError: Direct assignment to the reverse side of a related set is prohibited. Use username.set() instead.

这是我的模型.py:

class PostComments(models.Model):
post = models.ForeignKey(InfoPost, on_delete=models.CASCADE, related_name='comments')
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name="username")
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
#approved_comment = models.BooleanField(default=False)
reply_to = models.ForeignKey("self", null=True, blank=True, on_delete=models.CASCADE, related_name='replies')

#def __str__(self): return 'Comment by {} on {}'.format(self.name, self.post)

我正在尝试为我的网站上的帖子制作评论系统,但是出现了一些错误。我不知道我的模型有什么问题,但如果我尝试在没有我的新 PostComments 模型的情况下进行迁移,它的迁移非常好。

更新。这是我的代码,我在其中使用我的 PostComments 模型

forms.py
class CommentsForm(forms.ModelForm):
post = forms.CharField(required=False)
author = forms.CharField(required=False)

class Meta:
model = PostComments
fields = '__all__'

View .py

def post(request, slug):
post = get_object_or_404(InfoPost, slug=slug)
comment_list = CommentsForm._meta.model.objects.all()

if request.user.is_authenticated:
user = User.objects.get(username=request.user.username)

if request.method == 'POST':
comment = CommentsForm(request.POST)
print(comment.is_valid())
if comment.is_valid():
com = comment.save(commit=False)
com.author = request.user
com.post = post
com.save()
return redirect('post', slug=slug)
else:
comment_form = CommentsForm()
return render_to_response('MainPage/Post.html',
{'post': post, "user": user, 'img_w': image_w, 'img_h': image_h,
'comments_list': comment_list, 'comment_form': comment_form})

return render_to_response('MainPage/Post.html', {'post': post, 'img_w': image_w, 'img_h': image_h, 'comments_list': comment_list})

我在想,它是在我试图获取所有评论对象以在 View 中显示它们之后开始的,但我真的不知道

最佳答案

您正在使用作者 ForeignKey 上的 related_name 屏蔽 User 模型上的实际 username 字段。

应该是这样的:

author = models.ForeignKey(User, on_delete=models.CASCADE, related_name="usernames")

'related_name=usernames' 复数形式。

它失败的原因是因为当你这样做的时候:

user = User.objects.get(username=request.user.username)

您基本上是在访问与用户关联的评论集的反向关系,而不是实际的 username 字段(我假设它是一个 CharField)。

有关 django documentation 中 related_name 的更多信息.

关于python - Django:禁止直接赋值给相关集合的反面。使用 username.set() 代替,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53803394/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com