嵌入式documen的mongoengine update listfield

2024-10-02 04:27:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试更新mongoengine中嵌入文档的列表字段。我已经看过几乎所有相关的问题,但不知何故我的代码就是不起作用。我知道我少了一些小东西,但我想不出来。在

这是我的代码:

文件:

class Address(EmbeddedDocument):
    line1 = StringField(required=True, max_length=63)
    line2 = StringField(max_length=127)
    pin = StringField(max_length=6, min_length=6)


class Store(EmbeddedDocument):
    code = StringField(max_length=50, required=True)
    store_name = StringField(max_length=255, required=True)
    address = EmbeddedDocumentField(Address)


class Vendor(Document):
    name = StringField(max_length=255, required=True)
    stores = ListField(EmbeddedDocumentField(Store))

视图:

^{pr2}$

以下是我打印报表的输出:

<QueryDict: {u'oper': [u'edit'], u'code': [u'BSK'], u'pin': [u'1'], u'line2': [u'near huda city center'], u'line1': [u'Shushant Lok'], u'store_name': [u'Baskin'], u'id': [u'2']}>

address new near huda city center 1

new store near huda city center 1
updated 0

我的更新存储方法不起作用。请帮忙。在


Tags: store代码nametruecityaddressrequiredlength
1条回答
网友
1楼 · 发布于 2024-10-02 04:27:46

好吧,我现在可以用我的代码了。 我修改了我的update_store方法,它成功了。在

def update_store(self, post_dict):
        vendor = self
        new_address = Address(line1=post_dict.get('line1'), line2=post_dict.get('line2'))
        if post_dict.get('pin') != 'None':
            new_address.pin = post_dict.get('pin')
        new_store = Store(code=post_dict.get('code'), store_name=post_dict.get('store_name'), address=new_address)
        index = int(post_dict.get('id')) -1
        stores = vendor.get_active_stores()
        stores[index].is_active = False
        vendor.stores.append(new_store)
        vendor.save()

相关问题 更多 >

    热门问题