2014-11-15 2 views
-1

Я пытаюсь обновить список встроенных документов в mongoengine. Я прошел почти все связанные вопросы, но каким-то образом мой код просто не сработает. Я знаю, что есть что-то маленькое, что мне не хватает, но я не могу понять.mongoengine update listfield встроенного документа

Вот мой код:

Документы:

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

ОК Итак, я получил свой код для работы сейчас. Я изменил свой метод 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()