在Django中根据布尔值排除模型的行。

huangapple go评论53阅读模式
英文:

Excluding rows from a model depending on Boolean values in Django

问题

以下是翻译好的内容:

我有一个帖子模型和用户模型
用户有一个包含其信息和其帖子的个人资料页面。
帖子模型如下:

# models.py
class Post(models.Model):
    owner = models.ForeignKey(User, on_delete=models.CASCADE, null=False)
    title = models.CharField(max_length=255, verbose_name="Post Title")
    slug = models.SlugField(null=True, blank=True, unique=False)
    content = QuillField()
    topic = models.ForeignKey(Topic, on_delete=models.SET_NULL, null=True, blank=True)
    is_published = models.BooleanField(null=False, default=True)
    likes = models.ManyToManyField(User, related_name="post_likes", blank=True)
    updated_at = models.DateTimeField(auto_now=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

    def save(self, *args, **kwargs):
        if self.slug is None:
            self.slug = slugify(self.title)

        super().save(*args, **kwargs)
# views.py
class UserView(TemplateView):
    def get(self, request, username=None):
        if username == None:
            user = User.objects.get(username=request.user.username)
        else:
            user = User.objects.get(username=username)

        posts = Post.objects.filter(owner__username=username).all()

        context = {"user": user, "posts": posts}
        return render(request, "base/user_view.html", context)

用户可以创建帖子,但可以将其保存为草稿或发布。我想要实现的是,当用户转到他们的个人资料时,他们可以看到已发布的所有帖子以及已保存为草稿的帖子,但其他用户只能看到该用户已发布的帖子。

如何在个人资料模板中实现这一点?或者是否可以在views.py中完成?

英文:

I have a post model and user model
The user has a profile page that contains their info and their posts.
The Post model is like this:

# models.py
class Post(models.Model):
    owner = models.ForeignKey(User, on_delete=models.CASCADE, null=False)
    title = models.CharField(max_length=255, verbose_name="Post Title")
    slug = models.SlugField(null=True, blank=True, unique=False)
    content = QuillField()
    topic = models.ForeignKey(Topic, on_delete=models.SET_NULL, null=True, blank=True)
    is_published = models.BooleanField(null=False, default=True)
    likes = models.ManyToManyField(User, related_name="post_likes", blank=True)
    updated_at = models.DateTimeField(auto_now=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

    def save(self, *args, **kwargs):
        if self.slug is None:
            self.slug = slugify(self. Title)

        super().save(*args, **kwargs)
# views.py
class UserView(TemplateView):
    def get(self, request, username=None):
        if username == None:
            user = User.objects.get(username=request.user.username)
        else:
            user = User.objects.get(username=username)

        posts = Post.objects.filter(owner__username=username).all()

        context = {"user": user, "posts": posts}
        return render(request, "base/user_view.html", context)

The user can create a post but can save it as a draft or published it. What I want to achieve is that the user goes to their profile they can see the all their posts that have been published and the ones that have been saved as a draft, but other users can only see the published posts of that user.

How can I achieve this in the profile template? or can it be done in the views.py?

答案1

得分: 1

在筛选中简单地加入已发布的条件:

posts_own = Post.objects.filter(owner__username=username)....
posts_others= Post.objects.exclude(owner__username=username).filter(is_published=True)....
英文:

simply add the published condition to the filter:

posts_own = Post.objects.filter(owner__username=username)....
posts_others= Post.objects.exclude(owner__username=username).filter(is_published=True)....

huangapple
  • 本文由 发表于 2023年5月6日 17:11:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定