O
O
Oleg Vedernikov2015-12-30 14:40:48
Django
Oleg Vedernikov, 2015-12-30 14:40:48

How to specify the associated model?

views.py

class Comment(CreateView):
    model = Comment
    form_class = CommentForm
    template_name = 'article_detail.html'
    success_url = '/'

    def form_valid(self, form):
        instance = form.save(commit=False)
        instance.comment_article = Article.objects.get(id=1)
        instance.save()
        return super(Comment, self).form_valid(form)

Tell me how to define the id argument so that it points to the current article?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey K, 2015-12-30
@kila

instance.comment_article = Article.objects.get(id=self.kwargs['article_id'])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question