开发者

Modelform validates and saves successfully in view...and yet it doesn't

HI,

This is an unexpected result for me. I do the following in my view....

        (standard view stuff here...)

        if post:
            postform=PostForm(request.POST,instance=post, prefix="post")
            tagform=TagForm(request.POST, instance=post,prefix='tags')  
            ohouse_form=Host_OHouseForm(request.POST,instance=post,prefix="ohouse")
        else:   
            postform=PostForm(request.POST,prefix="post")
            tagform=TagForm(request.POST, prefix='tags')
            ohouse_form=Host_OHouseForm(request.POST,prefix="ohouse")   

                   (....I first validate that postform and tagform are valid...) 
        if ohouse_form.is_valid():
            ohouse=ohouse_form.save(commit=False)
            ohouse.post=post
            ohouse.host=user
            ohouse.save()

The form in question is a modelform. What's confusing me is that the the view is working properly in that the form validates and the save is successful. The model should save at that point...if there were anything wrong with the form/model, it would either not validate or raise an error.

And yet the instance isn't showing开发者_StackOverflow社区 up in my admin backend. It's also not present in the ohouse queryset.

This is perplexing me


Based on your latest update:

That's super promising progress. That means database writes are happening, as returning that ID is a DB query.

You should print type(ohouse) as well. I have a feeling you're not saving an OHouse object.

I see you're passing in instance=post to each of your ModelForms, which can't be right. That would make Host_OHouseForm({}, instance=post) return a new Post object.

I've got my bet that's the problem.

  if post:
        postform=PostForm(request.POST,instance=post, prefix="post")
        tagform=TagForm(request.POST, instance=post,prefix='tags')  
        ohouse_form=Host_OHouseForm(request.POST,instance=post,prefix="ohouse")


I notice that you are using prefix. Check out the strange behaviour I noticed in this other answer.

Python - Django - Form choicefield and cleaned_data

edit:

are you meaning to set all your instance=post, even for your OHouse form?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜