From: Douglas Bagnall Date: Wed, 13 Aug 2025 05:19:16 +0000 (+1200) Subject: python:models: do not re-use mutable defaults X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=439146c7a0ff362e2247feb94f2228edccea36be;p=thirdparty%2Fsamba.git python:models: do not re-use mutable defaults This ensures that model.save works when a field has the many flag set, but the object has no attribute of that name, and the caller appends to the attribute list, like this: user.key_credential_link.append(link) When we get to save, and are doing this: value = getattr(self, attr) old_value = getattr(existing_obj, attr) if value != old_value: # commit the change the .append() will have added the item to both value and old_value because they are the same list. But not any more. This was a problem because the Field instance is attached to the model class, not the model instance. Signed-off-by: Douglas Bagnall Reviewed-by: Gary Lockyer --- diff --git a/python/samba/domain/models/fields.py b/python/samba/domain/models/fields.py index cff11661e73..959f80faf5c 100644 --- a/python/samba/domain/models/fields.py +++ b/python/samba/domain/models/fields.py @@ -64,7 +64,7 @@ class Field(metaclass=ABCMeta): # This ensures that fields with many=True are always lists. # If this is inconsistent anywhere, it isn't so great to use. if self.many and default is None: - self.default = [] + self.default = lambda x: list() else: self.default = default