0

I want to use the generic django ListView to display all the posts of the logged user through this url:

#/users/myposts/
url(r'^myposts/', views.MyPostsView.as_view(), name='myposts'),

My Post model is as following:

class Post(models.Model):
   person = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
   domain = models.ForeignKey(Domain, on_delete=models.CASCADE)
   speciality = models.ForeignKey(Speciality, on_delete=models.CASCADE)
   level = models.ManyToManyField(Level)
   date_post = models.DateTimeField()
   title = models.CharField(max_length=200)
   description = models.CharField(max_length=1000)
   is_deleted = models.BooleanField(default=False)
   date_delete = models.DateTimeField(null=True)

and my generic ListView is:

class MyPostsView(generic.ListView):
     template_name = 'posts_list.html'
     userId = None

     def get_queryset(self):
         return Post.objects.get(person=self.request.user.id)

doing this i get the following error message

get() returned more than one Post -- it returned 2

but when I return Post.objects.all() it returns all the posts and displays them

1 Answer 1

1

Use objects.filter instead of objects.get.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.