Reputation: 49
I want to establish a queryset of Post
model which filters only the published=True
in the Public
view.
I have tried the following:
return Post.objects.filter('published'==True).all()
views.py
class PublicList(ListView):
template_name = 'publish.html'
context_object_name = 'items'
model = Post
def get_queryset(self):
return Post.objects.filter('published'==True).all()
models.py
class Post(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
description = models.TextField()
created_on = models.DateTimeField(default=timezone.now)
published = models.BooleanField(default=False)
def publish(self):
self.published=True
self.save()
def unpublish(self):
self.published=False
self.save()
def __str__(self):
return self.title
The traceback errors:
File "C:\Users\AngryBuLLz\Desktop\Django\prac_18\firstapp\views.py" in get_queryset
82. return Post.objects.filter('published'==True).all()
File "C:\Users\AngryBuLLz\AppData\Local\conda\conda\envs\madeenv\lib\site-packages\django\db\models\manager.py" in manager_method
82. return getattr(self.get_queryset(), name)(*args, **kwargs)
Upvotes: 0
Views: 1183