El 24/04/15 06:08, David escribió:
This appears to work. Using a view for 2 functions seems pretty fugly though. Are there better ways to achieve this?

class CreateComment(ListView, FormMixin):
model = Comment
paginate_by = 2
form_class = CommentForm

def post(self, request, *args, **kwargs):
form = CommentForm(self.request.POST)
if form.is_valid():
obj = form.save(commit=False)
obj.creator = request.user
obj.object_id = self.kwargs['pk']
obj.content_type_id = self.kwargs['ct']
obj.save()
else:
return super(CreateComment, self).post(request, *args, **kwargs)

def get_queryset(self):
comments = Comment.objects.filter(
object_id=self.kwargs['pk'], content_type_id=self.kwargs['ct']
)
return comments

def get_context_data(self, **kwargs):
context = super(CreateComment, self).get_context_data(**kwargs)
context['form'] = CommentForm
return context

I think that you shoul use CreateView to create new objects or UpdateView to update them.
ListView is used to represent a list of objects.

Cheers,
Felix.

--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/553A33D6.3070407%40epepm.cupet.cu.
For more options, visit https://groups.google.com/d/optout.

Reply via email to