与wtforms_alchemy.ModelFieldList的一对多关系不会更新子项

时间:2018-04-25 03:45:55

标签: sqlalchemy wtforms

下面是一个填充空父对象并创建其子对象的表单。有必要手动调用ModelFormField,这是一个小麻烦。它很棒。但是,当我使用表单执行更新时,只更新对象 - 子项是新创建的。

在此框架中传播子项更新的正确方法是什么?实际上,我想在下面的两个打印声明中打印相同的内容。如果formdata为孩子们提供了额外的(缺少的)数据,那么表单会创建(删除)子项,我将特别感激。

from multidict import MultiDict
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine

from wtforms_alchemy import ModelFieldList, ModelForm, ModelFormField

Base = declarative_base()

class Child(Base):
  __tablename__ = "child"
  id = Column(Integer, primary_key=True)
  name = Column(String)
  parent_id = Column(Integer, ForeignKey("parent.id"))

class Parent(Base):
  __tablename__ = "parent"
  id = Column(Integer, primary_key=True)
  name = Column(String)
  children = relationship(Child)

class ChildForm(ModelForm):
  class Meta:
    model = Child

class ParentForm(ModelForm):
  class Meta:
    model = Parent
  children = ModelFieldList(ModelFormField(ChildForm))  # annoyed!

engine = create_engine("sqlite://")
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()

# Form data
formdata = MultiDict({"name": "Foo", "children-0-name": "hotdog"})

# Create initial object
question = Parent()
form = ParentForm(formdata, obj=question)
form.populate_obj(question)
if not form.validate():
  raise RuntimeError(form.errors)
session.add(question)
session.commit()

# prints: "(1, 1)"
print((question.id, question.children[0].id))


# Retrieve object and update with same data
question_get = session.query(Parent).get(question.id)
form = ParentForm(formdata, obj=question_get)
form.populate_obj(question_get)
if not form.validate():
  raise RuntimeError(form.errors)
session.add(question_get)
session.commit()

# prints: "(1, 2)", want it to print the same as above
print((question_get.id, question_get.children[0].id))

2 个答案:

答案 0 :(得分:1)

在挖掘代码时,看起来似乎需要对象ID进行更新。因此,必须使用包含id的更新表单并传递ID。

--- orig.py 2018-05-01 20:51:09.000000000 -0700
+++ fixed.py    2018-05-01 20:56:16.000000000 -0700
@@ -4,6 +4,7 @@
 from sqlalchemy.orm import relationship
 from sqlalchemy.orm import sessionmaker
 from sqlalchemy import create_engine
+from wtforms import IntegerField

 from wtforms_alchemy import ModelFieldList, ModelForm, ModelFormField

@@ -24,6 +25,7 @@
 class ChildForm(ModelForm):
   class Meta:
     model = Child
+  id = IntegerField()  # Optional field used in update operations.

 class ParentForm(ModelForm):
   class Meta:
@@ -53,7 +55,11 @@

 # Retrieve object and update with same data
 question_get = session.query(Parent).get(question.id)
-form = ParentForm(formdata, obj=question_get)
+child_id = question_get.children[0].id
+formdata_update = MultiDict({"name": "Foo",
+                             "children-0-id": child_id,
+                             "children-0-name": "pizza"})
+form = ParentForm(formdata_update, obj=question_get)
 form.populate_obj(question_get)
 if not form.validate():
   raise RuntimeError(form.errors)

答案 1 :(得分:0)

好听。但这似乎仍然很麻烦。随后,您是否找到了一种简便的方法来对具有一对多关系的表单进行更新? WTForms应该节省开发人员的时间。但是,这似乎比没有WTForms时要花费更多。