@@ -149,6 +149,19 @@ def __repr__(self):
149
149
class TombstoneField (SystemField ):
150
150
"""System field for accessing a record's deletion status."""
151
151
152
+ def mark_to_be_removed (self , instance ):
153
+ """Mark to be removed."""
154
+ if not hasattr (instance , "_obj_cache" ):
155
+ instance ._obj_cache = {}
156
+ instance ._obj_cache ["remove" ] = True
157
+
158
+ def is_marked_to_be_removed (self , instance ):
159
+ """Is marked to be removed."""
160
+ if not hasattr (instance , "_obj_cache" ):
161
+ instance ._obj_cache = {}
162
+
163
+ return instance ._obj_cache .get ("remove" , False )
164
+
152
165
#
153
166
# Data descriptor methods (i.e. attribute access)
154
167
#
@@ -165,6 +178,9 @@ def get_obj(self, record, owner=None):
165
178
if tombstone is not None :
166
179
return tombstone
167
180
181
+ if self .is_marked_to_be_removed (record ):
182
+ return None
183
+
168
184
ts_dict = record .get ("tombstone" , None )
169
185
tombstone = Tombstone (ts_dict ) if ts_dict else None
170
186
self ._set_cache (record , tombstone )
@@ -178,6 +194,7 @@ def set_obj(self, record, value):
178
194
"""Set obj."""
179
195
if value is None :
180
196
tombstone = None
197
+ self .mark_to_be_removed (record )
181
198
elif isinstance (value , dict ):
182
199
tombstone = Tombstone (value )
183
200
elif isinstance (value , Tombstone ):
@@ -187,11 +204,16 @@ def set_obj(self, record, value):
187
204
188
205
self ._set_cache (record , tombstone )
189
206
207
+ def __delete__ (self , record ):
208
+ """Remove tombstone."""
209
+ self .mark_to_be_removed (record )
210
+ self ._set_cache (record , None )
211
+
190
212
def pre_commit (self , record ):
191
213
"""Dump the configured tombstone before committing the record."""
192
214
tombstone = self .get_obj (record )
193
-
194
215
if tombstone :
195
216
record ["tombstone" ] = tombstone .dump ()
196
- else :
217
+
218
+ if self .is_marked_to_be_removed (record ):
197
219
record .pop ("tombstone" , None )
0 commit comments