Hi there, I'm having trouble in filtering objects of a model based on another model's foreign key. The scenario is, I've got a forum homepage and when a topic is clicked, it opens its DetailView. Now if I try to get all answers in the database there's no issue, but I want to get only the answers which are there only for that particular topic.
here are my project files views.py from django.views import generic > from django.views.generic.edit import CreateView > from .models import Topic, Answer > from .forms import TopicForm > from django.contrib.auth.decorators import login_required > > class IndexView(generic.ListView): > template_name = 'index.html' > context_object_name = 'topic_list' > def get_queryset(self): > return Topic.objects.all() > > class TopicDetailView(generic.DetailView): > model = Topic > context_object_name = 'topic' > template_name = 'topicdetail.html' > def get_context_data(self, **kwargs): > context = super(TopicDetailView, self).get_context_data(**kwargs) > context["answer"] = Answer.objects.filter(<can't figure out this>) > return context > class TopicCreateView(CreateView): > model = Topic > form_class = TopicForm > template_name = 'topic_form.html' models.py from django.db import models > from oauth.models import UserProfile > from ckeditor_uploader.fields import RichTextUploadingField > from django.core.urlresolvers import reverse > > class Topic(models.Model): > # Choices > CAT_CHOICES = ( > ('Q', 'Question'), > ('F', 'Feedback'), > ) > # Topic Database Model > owner = models.ForeignKey(UserProfile, on_delete=models.CASCADE) > category = models.CharField(max_length=3, choices=CAT_CHOICES, > default='Q') > title = models.CharField(max_length=256) > content = RichTextUploadingField(blank=True) > slug = models.SlugField(unique=True) > views = models.PositiveIntegerField(default=0) > answers = models.PositiveIntegerField(default=0) > tags = models.CharField(max_length=50) > created_at = models.DateTimeField(auto_now_add=True) > def get_absolute_url(self): > return reverse('forum:detail', kwargs={'pk': self.pk}) > def __str__(self): > return self.title > > class Answer(models.Model): > topic = models.ForeignKey(Topic, on_delete=models.CASCADE) > owner = models.ForeignKey(UserProfile, on_delete=models.CASCADE) > content = RichTextUploadingField(blank=True) > created_at = models.DateTimeField(auto_now_add=True) > total_votes = models.SmallIntegerField(default=0) urls.py from django.conf.urls import url > from forum import views > from django.contrib.auth.decorators import login_required > app_name = 'forum' > urlpatterns = [ > url(r'^$', login_required(views.IndexView.as_view()), name='index'), > url(r'^topic/(?P<pk>[0-9])/$', > login_required(views.TopicDetailView.as_view()), name='detail'), > url(r'^topic/add/$', login_required(views.TopicCreateView.as_view()), > name='add_topic'), > ] What do I need to do to accomplish the same? -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/dc049d19-2379-439b-8b13-50292a697b04%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.