且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

未保存的实例的表单

更新时间:2023-02-12 11:19:20

muntu -
粘贴选项1,即使它需要一点工作 - 这是一个更好的方法,并提供更多的灵活性。我认为问题在于 friend_of 字段在 Friend - 你已经成为必填字段,所以你无法保存 Friend ,但您也不会在代码中为其提供值。 (参见 django在线文档。)

muntu -- Stick with option 1, even if it needs to be worked on a bit -- it's a sounder approach and offers more flexibility going forward. I think the problem is with the friend_of field on Friend -- you've made it a required field, so you can't save a Friend without it, but you also don't supply a value for it in your code. (See the advisory note in the django online documentation.)

使用这些指导原则,在验证/保存部分尝试此代码:

Using those guidelines, try this code in your validation/save section:

if form.is_valid():
    friend = form.save(commit=False)
    friend.friend_of = userprofile # (or whatever the user variable is)
    friend.save()

作品或出现什么问题。