fix changeset when dummyvalues end up being used

This commit is contained in:
Laura Klünder 2024-12-15 21:25:32 +00:00
parent 63b2fc21a7
commit 5d6cf7e052
2 changed files with 12 additions and 5 deletions

View file

@ -682,6 +682,8 @@ class ChangedObjectCollection(BaseSchema):
# store the dummyvalue so we can tell the user about it
problems.get_object(new_operation.obj).dummy_values[field_name] = dummy_value
new_operation.fields[field_name] = dummy_value
else:
# we have set this field to a non-dummy value, if we used one before we can forget it
problems.get_object(new_operation.obj).dummy_values.pop(field_name, None)

View file

@ -252,14 +252,18 @@ class PrefetchedDatabaseOperationCollection:
def apply(self):
# todo: what if unique constraint error occurs?
prev = self.operations.prev.model_copy(deep=True)
for operation in self.operations:
if isinstance(operation, (CreateObjectOperation, CreateMultipleObjectsOperation)):
self.instances.update(operation.apply_create())
sub_ops = operation.objects if isinstance(operation, CreateMultipleObjectsOperation) else [operation]
for sub_op in sub_ops:
prev.set(ref=sub_op.obj, values=sub_op.fields, titles=None)
else:
prev_obj = self.operations.prev.get(operation.obj)
prev_obj = prev.get(operation.obj)
if prev_obj is None:
print('WARN WARN WARN')
values = prev_obj.values
raise ValueError(f'Missing: {operation.obj}')
try:
instance = self.instances[operation.obj]
except KeyError:
@ -267,5 +271,6 @@ class PrefetchedDatabaseOperationCollection:
instance = apps.get_model("mapdata", operation.obj.model).filter(pk=operation.obj.id).first()
else:
instance = None
if instance is not None:
operation.apply(values=values, instance=instance)
if instance is None:
raise ValueError('Instance to update doesn\'t exist')
operation.apply(values=prev_obj.values, instance=instance)