user10605113
user10605113

Reputation: 211

How can I have two objects from two models in one view

I am writing django blog app and I have problem how I can get Comment object in Post view. With post object there is no problem because I write self.get_object() and done. And the question is how I can get Comment object. Below is my code.

Here is view.

class PostDetail(generic.DetailView, FormMixin):
    template_name = 'post_detail.html'
    context_object_name = 'post'
    model = Post
    form_class = CommentForm

Here is post model:

class Post(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    title = models.CharField(max_length=200)
    text = RichTextUploadingField()

Here is comment model

class Comment(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
    text = RichTextUploadingField()

Upvotes: 0

Views: 2044

Answers (3)

Hossein Kalbasi
Hossein Kalbasi

Reputation: 1861

I don't know what exactly you want to do!, but from what is asked, I think one way is to use a function-based view like this (I didn't try it, but I expect this or something very similar to work for you):

from django.shortcuts import render

def myview(request):
    post_objs = Post.objects.all()
    comment_objs = Comment.objects.all()

    mydict = {'posts':post_obj, 'comments':comment_obj}
    return render(request, 'myapp/myhtml.html', context=mydict)

considering the one-to-many relationship you have, you can also go further and pass the exact data you want

def myview(request, pk):
    the_post_obj = Post.objects.get(pk=pk)
    comment_objs = Comment.objects.filter(post=the_post_obj)

    mydict = {'the_post':the_post_obj,'comments':comment_objs}
    return render(request, 'myapp/myhtml.html', context=mydict)

With whatever view you wrote, you can now go to the HTML file and display the queried data in whatever way you want. For instance, you can show all comments of a single post. Don't forget that for using the second view I wrote above, you have to send the post PK to the view (for example you can put it in a link in the HTML file so that when the user clicks on the post (which is a hyperlink), the post PK goes through the view function, and the relevant data is displayed) .

Upvotes: 1

Rohit Garg
Rohit Garg

Reputation: 71

You can get the comment object by overriding the get_context_data in detailView as below -

def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['comment'] = self.get_object().comments.all() return context

Upvotes: 0

danielmoessner
danielmoessner

Reputation: 379

You could change the DetailView into a TemplateView and overwrite the get_context_data(). See: http://ccbv.co.uk/

Or you could get the post related comments in your template with a for loop :

for comment in self.get_object().comments.all
    # do sth with the comment

Upvotes: 0

Related Questions