我正在尝试更新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))
查看:
def stores(request, *args, **kwargs):
.......
elif request.method == 'POST':
if request.is_ajax():
if request.POST.get('oper') == 'edit':
post_dict = request.POST
# updated_vendor = Vendor.objects(pk=vendor_pk, stores__$__)
vendor.update_store(vendor_pk, post_dict)
return get_json_response({'msg': 'Store details updated successfully!!'})
else:
....
def update_store(self, vendor_pk, post_dict):
print post_dict
store = [attr for attr in self.stores if attr.code == post_dict.get('code') and attr.store_name == post_dict.get('store_name')]
vendor = self
new_address = Address(line1=post_dict.get('line1'), line2=post_dict.get('line2'),
city=post_dict.get('city'), state=post_dict.get('state'),
country=post_dict.get('country'))
if post_dict.get('pin') != 'None':
new_address.pin = post_dict.get('pin')
print "address new", new_address.line2, new_address.pin
new_store = Store(code=post_dict.get('code'), store_name=post_dict.get('store_name'), address=new_address)
print "new store", new_store.address.line2, new_store.address.pin
if store:
index = vendor.stores.index(store[0])
updated = Vendor.objects(pk=vendor_pk, stores__code=post_dict.get('code'), stores__name=post_dict.get('store_name')).update_one(set__stores__index=new_store)
print "updated", updated
#print index,vendor.stores[index].address.city
# del vendor.stores[index]
# vendor.stores.append(new_store)
# vendor.stores[index].code = post_dict.get('code')
# vendor.stores[index].store_name = post_dict.get('store_name')
# vendor.stores[index].address.line1 = post_dict.get('line1')
# vendor.stores[index].address.line2 = post_dict.get('line2')
# if post_dict['pin'] != 'None':
# vendor.stores[index].address.pin = post_dict.get('pin')
# vendor.save()
这些是我的印刷声明'输出:
<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
我的update_store方法刚刚开始工作。请帮助。
答案 0 :(得分:0)
好的,所以我现在让我的代码工作了。 我将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()