theenglishway (time) 6 سال پیش
والد
کامیت
4017a84c5a
2فایلهای تغییر یافته به همراه7 افزوده شده و 3 حذف شده
  1. 1 1
      planner/cli.py
  2. 6 2
      planner/models/mixins.py

+ 1 - 1
planner/cli.py

@@ -41,8 +41,8 @@ def save_or_display_errors(model, data):
     ok, m_doc = model.validate(data)
     if ok :
         m = model.get(m_doc)
-        click.secho(f"{m} was successfully added", fg='green')
         m.save()
+        click.secho(f"{m} was successfully added", fg='green')
     else:
         click.secho(f"Errors : {m_doc}", fg='red')
 

+ 6 - 2
planner/models/mixins.py

@@ -70,7 +70,10 @@ class DbMixin:
             # The cache is updated a first time so that instances looking up
             # for this instance during the load_reverse can find it
             kls.cache[key] = instance
-            instance.id = document.doc_id
+            try:
+                instance.id = document.doc_id
+            except AttributeError:
+                ...
             instance.load_reverse()
             # The cache value is updated with its final value
             kls.cache[key] = instance
@@ -86,4 +89,5 @@ class DbMixin:
 
     def save(self):
         self.document.update({'uuid': str(uuid.uuid4())})
-        db.table(self.class_name()).insert(self.document)
+        self.id = db.table(self.class_name()).insert(self.document)
+        self.cache[self._get_cache_key(self.document)] = self