【发布时间】:2021-04-29 14:44:48
【问题描述】:
这是我的models.py
class Article(models.Model):
title = models.CharField(max_length=100) #The title of the article
# This is to count the number of users who have viewed the article
viewed_user = models.ManyToManyField(User,related_name='viewed_user',blank=True)
我正在尝试在 views.py 中添加逻辑,即当用户访问特定文章时,如果用户不在viewed_user 列表中,则将该用户添加到那里。
这是我在views.py中尝试过的
article = get_object_or_4040(Article,id=id)
if user in article.viewed_user.get_queryset():
print("User has already viewed the article")
else:
article.viewed_user.create(user=user) # THrows an error
但是当我尝试、更新、创建、设置、附加......时,我遇到了很多类型的错误。
任何帮助将不胜感激。
【问题讨论】:
-
如果您包含完整的可重现代码以及您遇到的确切错误,将会更容易。
-
其他一切正常,只是当用户打开文章时将用户对象添加到viewed_user列表中的部分
标签: django django-models django-views model django-queryset