Sqlalchemy散装插入金字塔

时间:2013-03-16 10:36:46

标签: python sqlalchemy pyramid

我有一个对象需要使用循环插入几次。每次循环迭代我都会改变一些字段值。 我的代码是:

@view_config(route_name="be.bautocredits.rates.add",
             request_method="POST",
             permission="admin",
             renderer="json")
def _add_rates(self):
    _ = self.request.translate
    controls = self.request.POST.items()
    schema = AutocreditRateSchema()
    try:
        schema.deserialize(controls)
        rate = AutocreditRate()
        rate.autocredit = Autocredit.by_rid(self.request.POST.get('_autocredits_rid'))
        rate.carshow = Carshow.by_rid(self.request.POST.get('_carshows_rid'))
        rate.currency = Currency.by_rid(self.request.POST.get('_currencies_rid'))
        rate.min_cost_currency = Currency.by_rid(self.request.POST.get('min_cost_currencies_rid'))
        rate.max_cost_currency = Currency.by_rid(self.request.POST.get('max_cost_currencies_rid'))
        rate.min_currency = Currency.by_rid(self.request.POST.get('min_currencies_rid'))
        rate.max_currency = Currency.by_rid(self.request.POST.get('max_currencies_rid'))
        rate.manufacturer = Manufacturer.by_rid(self.request.POST.get('_manufacturers_rid'))
        rate.autocredit.updated = datetime.datetime.now()

        _cars_rids = list(set(self.request.POST.getall('_cars_rid')))
        if _cars_rids:
            for _cars_rid in _cars_rids:
                rate.car = Car.by_rid(_cars_rid)
                DBSession.add(rate)
        else:
            DBSession.add(rate)

    except colander.Invalid, e:
        return dict(errors=dict(e.asdict().items()+dict(status_bar=_(u'Check errors')).items()))
    return dict()

此代码仅插入单行。由于某些原因,我不能直接在“try block”中设置循环开始。 我做错了什么?

1 个答案:

答案 0 :(得分:3)

DBSession.add向会话添加一个对象(顾名思义) - 当时没有发出SQL查询,.add只是让会话知道该对象。

您可以继续修改对象的属性,直到提交会话 - 无论您将对象添加到会话的次数,只有一行将添加到数据库中。

可以能够做一些聪明的会话刷新你想要做的事情,但是,说真的,为什么不为每一行创建一个单独的对象?这就是ORM一般的工作方式。

def _add_rates(self):
    _ = self.request.translate
    controls = self.request.POST.items()
    schema = AutocreditRateSchema()
    try:
        schema.deserialize(controls)
        _cars_rids = list(set(self.request.POST.getall('_cars_rid')))
        autocredit = Autocredit.by_rid(self.request.POST.get('_autocredits_rid'))
        carshow = Carshow.by_rid(self.request.POST.get('_carshows_rid'))
        currency = Currency.by_rid(self.request.POST.get('_currencies_rid'))
        min_cost_currency = Currency.by_rid(self.request.POST.get('min_cost_currencies_rid'))
        max_cost_currency = Currency.by_rid(self.request.POST.get('max_cost_currencies_rid'))
        min_currency = Currency.by_rid(self.request.POST.get('min_currencies_rid'))
        max_currency = Currency.by_rid(self.request.POST.get('max_currencies_rid'))
        manufacturer = Manufacturer.by_rid(self.request.POST.get('_manufacturers_rid'))
        if _cars_rids:
            for _cars_rid in _cars_rids:
                rate = AutocreditRate(autocredit=autocredit, carshow=carshow, ...)
                rate.car = Car.by_rid(_cars_rid)
                rate.autocredit.updated = datetime.datetime.now()
                DBSession.add(rate)
        else:
            rate = AutocreditRate(autocredit=autocredit, carshow=carshow, ...)
            rate.autocredit.updated = datetime.datetime.now()
            DBSession.add(rate)