Skip to content

Commit 9177d72

Browse files
authored
Merge pull request #146 from kulik0v/update-new-field
Update new field fix
2 parents 07b0e86 + e807709 commit 9177d72

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

src/fireo/models/model.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,11 @@ class User(Model):
434434

435435
def __setattr__(self, key, value):
436436
"""Keep track which filed values are changed"""
437-
if key in self._field_list or not self._instance_modified:
438-
self._field_changed.append(key)
439-
else:
440-
self._field_list.append(key)
437+
self._field_changed.append(key)
438+
self._field_list.append(key)
439+
super(Model, self).__setattr__(key, value)
440+
441+
def _set_orig_attr(self, key, value):
442+
"""Keep track which filed values are changed"""
443+
self._field_list.append(key)
441444
super(Model, self).__setattr__(key, value)

src/fireo/queries/query_wrapper.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def from_query_result(cls, model, doc, nested_doc=False):
2424
# so mark it modified this will help later for figuring
2525
# out the updated fields when need to update this document
2626
setattr(model, '_instance_modified', True)
27+
2728
for k, v in doc_dict.items():
2829
field = model._meta.get_field_by_column_name(k)
2930
# if missing field setting is set to "ignore" then
@@ -41,7 +42,8 @@ def from_query_result(cls, model, doc, nested_doc=False):
4142
val = None
4243
else:
4344
val = field.field_value(v)
44-
setattr(model, field.name, val)
45+
# setattr(model, field.name, val)
46+
model._set_orig_attr(field.name, val)
4547

4648
# If parent key is None but here is parent key from doc then set the parent for this model
4749
# This is case when you filter the documents parent key not auto set just set it
@@ -56,7 +58,9 @@ def from_query_result(cls, model, doc, nested_doc=False):
5658
if model._meta.id is None:
5759
model._meta.id = ('id', IDField())
5860

59-
setattr(model, '_id', doc.id)
61+
# setattr(model, '_id', doc.id)
62+
model._set_orig_attr('_id', doc.id)
63+
6064
# save the firestore reference doc so that further actions can be performed (i.e. collections())
6165
model._meta.set_reference_doc(doc.reference)
6266
# even though doc.reference currently points to self, there is no guarantee this will be true
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from fireo.fields import TextField
2+
from fireo.models import Model
3+
4+
5+
class OldModel(Model):
6+
class Meta:
7+
collection_name = 'testcolletion'
8+
9+
first_name = TextField()
10+
# old Model contains no field 'last_name'
11+
12+
13+
class NewModel(Model):
14+
class Meta:
15+
collection_name = 'testcolletion'
16+
17+
first_name = TextField()
18+
# now we added 'last_name' field
19+
last_name = TextField()
20+
21+
22+
def test_update_new_field():
23+
old_instance = OldModel()
24+
old_instance.first_name = 'First'
25+
old_instance.save()
26+
27+
new_instance: NewModel = NewModel.collection.get(old_instance.key)
28+
new_instance.last_name = 'Last'
29+
new_instance.update()
30+
31+
got_instance = NewModel.collection.get(old_instance.key)
32+
assert got_instance.last_name == 'Last'

0 commit comments

Comments
 (0)