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[1]). 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[2] *must* be first).


-- 
Melvyn Sopacua

--------
[1] 
https://docs.djangoproject.com/en/1.11/ref/models/relations/#django.db.models.fields.related.Rel
atedManager
[2] 
https://docs.djangoproject.com/en/1.11/topics/auth/default/#the-loginrequired-mixin

-- 
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/5031538.fRoGy7vNcJ%40devstation.
For more options, visit https://groups.google.com/d/optout.

Reply via email to