Django 如何将其构建到模型中以检查用户是否阅读文章?
Django how do you build it into a model check that if User read articles or not?
首先,我在Django中有一个用户模型和一个文章模型每个用户可以write/read任何文章
我需要什么
想要一个每个用户都没有读过的文章列表。
想查看某篇文章的阅读人数和未阅读人数。
我如何构建或修改模型?
我的模型在这里
干杯
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(unique=True)
name = models.CharField(max_length=20, blank=True)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
date_joined = models.DateTimeField(auto_now_add=True)
objects = UserManager()
class article(models.Model):
title = models.CharField(max_length=255, blank=False)
content = models.TextField(max_length=1000, blank=False)
created_by = models.ForeignKey(
User,
on_delete=models.CASCADE
)
created_at = models.DateTimeField(auto_now_add=True)
(None 已测试,但应该有所帮助)
我希望在 User
和 article
之间添加 ManyToMany 关系来跟踪用户阅读的文章:
class User(...):
...
articles_read = models.ManyToManyField(article, related_name="read_by_user")
然后你可以做这样的事情来记录某人阅读了特定的文章:
a_user.articles_read.add(article)
I want a list of articles that each user has not read.
article.objects.exclude(read_by_user=user)
want to check the list of people who have read about a certain article
User.objects.filter(articles_read=article)
and the list of people who have not read it.
User.objects.exclude(articles_read=article)
文档 here 包括关于 'through models' 的注释,可以让您存储更多关于用户<->文章关系的信息,例如 when 例如,用户阅读了这篇文章。实际上,Rajat 的 Read
class 示例几乎可以用作您的直通模型
PS:我现在将您的 article
class 重命名为 Article
,以使生活更轻松 - Python 中的惯例是class 的首字母大写,而 class 的实例全部小写
这是我会做的。不确定这是否是最好的方法,但请尝试一下。
我将创建一个名为 'read'
的新模型
Class Read(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
article = models.ForeignKey(Article, on_delete=models.CASCADE)
现在,您要做的是,每当阅读一篇文章时,您都会创建一个新条目,例如:
read = Read.objects.create(user=user, article=article)
# you need to provide the user and the article objects
read.save()
此语句的作用是存储用户 user
已阅读文章 article
现在过滤阅读过你可能做的文章的用户
Read.objects.filter(article=**the article you wan't**)
查看未阅读文章列表,就是上面过滤条件中没有的用户。
现在检查每个用户已阅读的文章列表将是
Read.objects.filter(user=**the user you wan't**)
要检查那些用户没有读过的文章就是那些不在这个过滤器中的文章,你可以使用exclude()
方法/函数来做到这一点。
希望对你有所帮助。
首先,我在Django中有一个用户模型和一个文章模型每个用户可以write/read任何文章
我需要什么
想要一个每个用户都没有读过的文章列表。
想查看某篇文章的阅读人数和未阅读人数。
我如何构建或修改模型?
我的模型在这里
干杯
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(unique=True)
name = models.CharField(max_length=20, blank=True)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
date_joined = models.DateTimeField(auto_now_add=True)
objects = UserManager()
class article(models.Model):
title = models.CharField(max_length=255, blank=False)
content = models.TextField(max_length=1000, blank=False)
created_by = models.ForeignKey(
User,
on_delete=models.CASCADE
)
created_at = models.DateTimeField(auto_now_add=True)
(None 已测试,但应该有所帮助)
我希望在 User
和 article
之间添加 ManyToMany 关系来跟踪用户阅读的文章:
class User(...):
...
articles_read = models.ManyToManyField(article, related_name="read_by_user")
然后你可以做这样的事情来记录某人阅读了特定的文章:
a_user.articles_read.add(article)
I want a list of articles that each user has not read.
article.objects.exclude(read_by_user=user)
want to check the list of people who have read about a certain article
User.objects.filter(articles_read=article)
and the list of people who have not read it.
User.objects.exclude(articles_read=article)
文档 here 包括关于 'through models' 的注释,可以让您存储更多关于用户<->文章关系的信息,例如 when 例如,用户阅读了这篇文章。实际上,Rajat 的 Read
class 示例几乎可以用作您的直通模型
PS:我现在将您的 article
class 重命名为 Article
,以使生活更轻松 - Python 中的惯例是class 的首字母大写,而 class 的实例全部小写
这是我会做的。不确定这是否是最好的方法,但请尝试一下。
我将创建一个名为 'read'
的新模型Class Read(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
article = models.ForeignKey(Article, on_delete=models.CASCADE)
现在,您要做的是,每当阅读一篇文章时,您都会创建一个新条目,例如:
read = Read.objects.create(user=user, article=article)
# you need to provide the user and the article objects
read.save()
此语句的作用是存储用户 user
已阅读文章 article
现在过滤阅读过你可能做的文章的用户
Read.objects.filter(article=**the article you wan't**)
查看未阅读文章列表,就是上面过滤条件中没有的用户。
现在检查每个用户已阅读的文章列表将是
Read.objects.filter(user=**the user you wan't**)
要检查那些用户没有读过的文章就是那些不在这个过滤器中的文章,你可以使用exclude()
方法/函数来做到这一点。
希望对你有所帮助。