Thanks, Melvyn Sopacua,
Now it all makes sense.

On Saturday, 10 June 2017 21:54:57 UTC+5:30, Melvyn Sopacua wrote:
>
> On Saturday 10 June 2017 08:22:20 Ajat Prabha wrote:
>
>  
>
>  
>
> > 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>)
>
>  
>
> Use clear and consistent names. Since this can return multiple answers, 
> use answer_list, as in line with topic_list in your index view.
>
> this becomes:
>
> context['answer_list'] = self.object.answer_set.all()
>
>  
>
>  
>
>  
>
> > models.py
>
>  
>
> > 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)
>
>  
>
> Why is this here?
>
>  
>
> Is this what you want?
>
>  
>
> @property
>
> def number_of_answers(self):
>
> return self.answer_set.count()
>
>  
>
>  
>
> > > 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)
>
>  
>
> This creates a property 'answer_set' on the Topic model, which is a model 
> manager (to be precise, a RelatedManager 
> <https://docs.djangoproject.com/en/1.11/ref/models/relations/#django.db.models.fields.related.RelatedManager>).
>  
> So you can do queryset methods on it.
>
>  
>
>  
>
> > urls.py
>
>  
>
> > > url(r'^$', login_required(views.IndexView.as_view()),
>
>  
>
> Consider moving this to the view definitions:
>
>  
>
> class IndexView(LoginRequiredMixin, ListView):
>
>  
>
> (LoginRequiredMixin 
> <https://docs.djangoproject.com/en/1.11/topics/auth/default/#the-loginrequired-mixin>
>  
> *must* be first).
>
>  
>
>  
>
> -- 
>
> Melvyn Sopacua
>

-- 
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/45d6341f-0c84-4732-b7d6-bf0405cbc91d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to