Tuesday, July 28, 2015

Re: Using FormMixin with DetailView

I've reviewed your code a bit more and I see what you're trying to do, I think.

With that being said, I'll recant what I said about the CreateView.

Your trouble stems from the fact that FormView by default does not perform a form.save() operation anywhere (it only validates the form). CreateView does this in the form_valid() method.

I'm not sure what you are doing with the create_view() method in the form, but it looks like a pseudo-save method. You probably don't need it, but that chunk off code looks like it actually belongs in form_valid() on the view, not on the form.

Look into overriding form_valid() in your view. The example you were/are looking at is missing the save() functionality, and it states so at the beginning of the section.

"Again, let's assume we're not going to store this in a relational database but instead in something more esoteric that we won't worry about here."

I think that the docs are a bit misleading here, since I would expect a large section like this to at least make reference to something like "#save code here". Maybe it is in there, hard to tell on my phone.

- James

Are you sure you're using 1.7? Your original error posting begs to differ:

Django Version:1.6.11

The particular portion of docs that you are reading is meant to show how various mixin's can be used together to address specific edge cases, but other generic classes exist that are much better suited for what you are trying to do. Follow the link I posted to see examples.

The only 'tricky' portion of what you are trying to do is to attach the Comment to the Post, which is easily handled by overriding the form_valid() method in a CreateView.

You may not even need to define a Model Form.

- James

On Jul 28, 2015 1:39 AM, "Ioannis Latousakis" <latusaki@gmail.com> wrote:
Hello James,

The reason I am following this approach is because this is what was suggested in the documentation, for the scenario where you need to have a form within a DetailsView. Although even when I got my code to be identical to the documentation example, I still dont get an object created. Still, on form submit, the method get_success_url() of PostComment formview is called.  I am currently using django 1.7. 

On Tuesday, 28 July 2015 11:32:13 UTC+3, James Schneider wrote:

Is there a specific reason you aren't just using CreateView rather than trying to mix in a bunch of classes and trying to roll the post() logic yourself? You can probably cut a very large portion of code out and make this super simple.

https://docs.djangoproject.com/en/1.6/topics/class-based-views/generic-editing/#model-forms

You should also consider upgrading Django immediately to a newer version, as 1.6 is no longer supported.

-James

The override is this, I was experimenting with save..


class CommentForm(forms.ModelForm):
   
class Meta:
        model
= Comment
        exclude
= ("parent_post","created_at")


    def create_view(request, **kwargs):
        print "IN VIEW"
       
if request.method == "POST":
            parent_fk
= self.object
           
print "PARRENT"
           
print parent_fk
            form
= CommentForm(request.POST)
           
if form.is_valid():
                new_comment
= form.save(commit=False)
                new_comment
.parent_post = parent_fk
                new_comment
.save()
               
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))


--
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...@googlegroups.com.
To post to this group, send email to django...@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/6825b838-584c-4e89-bc4e-4a6d0bf81c5f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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+unsubscribe@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/CA%2Be%2BciV3_aZPWXGEayTWUb_Mt95XjnCom%2B7_2CbiPiyvcJXy5Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment