python - 使用 WTForms-Alchemy 更新具有一对多关系的对象(在 Pyramid 应用程序中)

标签 python sqlalchemy pyramid wtforms

我有一个带有一对多LocationsEvent类。当我在 WTForms-Alchemy 生成的表单中编辑 Location 的数据并填充每个文档的对象时,它似乎插入了一条新记录(具有正确的旧值)并将外键设置为 null 而不是更新现有记录:

[sqlalchemy.engine.base.Engine][Dummy-2] UPDATE location SET event_id=? WHERE location.id = ?
[sqlalchemy.engine.base.Engine][Dummy-2] (None, 1)
[sqlalchemy.engine.base.Engine][Dummy-2] INSERT INTO location (name, event_id) VALUES (?, ?)
[sqlalchemy.engine.base.Engine][Dummy-2] (u'loc2', 1)
[sqlalchemy.engine.base.Engine][Dummy-2] COMMIT

型号:

class Event(Base):
    __tablename__ = 'event'
    id = Column(Integer, primary_key=True)
    name = Column(Unicode(255), nullable=False)


class Location(Base):
    __tablename__ = 'location'
    id = Column(Integer, autoincrement=True, primary_key=True)
    name = Column(Unicode(255), nullable=True)

    event_id = Column(Integer, ForeignKey(Event.id))
    event = relationship(
        Event,
        backref='locations'  # the event needs to have this
    )

查看:

class LocationForm(ModelForm):
    class Meta:
        model = Location


class EventForm(ModelForm):
    class Meta:
        model = Event
    locations = ModelFieldList(ModelFormField(LocationForm))


class UserButton(Form):
    save_btn = wtforms.SubmitField('Save Data')

@view_config(route_name='ev', renderer='templates/ev.pt')
def ev(request):
    one = DBSession.query(Event).first()
    ev = EventForm(obj=one)
    ub = UserButton()
    if request.method == 'POST':
        new_uf = EventForm(request.POST)
        new_uf.populate_obj(one)
        transaction.commit()
    return {'ev':ev, 'ub':ub}

表格:

<form method="POST">

        <ul id="locations"><li><label for="locations-0">Locations-0</label> <table id="locations-0"><tr><th><label for="locations-0-name">name</label></th><td><input id="locations-0-name" name="locations-0-name" type="text" value="location1"></td></tr></table></li></ul>
        <br>

        <input id="name" name="name" required type="text" value="event1">
        <br>

<input id="save_btn" name="save_btn" type="submit" value="Save Data">

</form>

我在数据库的location表中看到了这一点:

id  name        event_id
1   location1   <null>
2   loc2        1

我该如何解决这个问题?也就是说,我想要更新 EventLocation 对象,而不是创建一个新对象? (即使使用相同的数据)。

最佳答案

您必须让Form知道它是一个现有对象:

obj = MyModel.query.get(1)
form = MyForm(obj=obj)
form.populate_obj(obj)
form.validate()

文档: http://wtforms-alchemy.readthedocs.org/en/latest/validators.html?highlight=existing#using-unique-validator-with-existing-objects

关于python - 使用 WTForms-Alchemy 更新具有一对多关系的对象(在 Pyramid 应用程序中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29578801/

相关文章:

c++ - boost.build 与 boost.python

python - 将 kivy 纹理转换为 opencv 图像

Python:SQLAlchemy ImportError:没有名为 pysqlite2 的模块

python - Ubuntu 加密主目录 | Errno 36 文件名太长

python - 在 Python 中将不同的日期数据规范化为单一格式

python - 按排序顺序检索 sqlalchemy 关系对象

Python SQLAlchemy 数据库迁移

frameworks - 在 Pyramid 中的表单 View 之间传递数据

python - Pyramid "model"也是 Pyramid "resource"吗?

javascript - 如何正确地将 jQuery 表与 Pyramid 模板集成?