]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
python:models: do not re-use mutable defaults
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Wed, 13 Aug 2025 05:19:16 +0000 (17:19 +1200)
committerDouglas Bagnall <dbagnall@samba.org>
Wed, 20 Aug 2025 04:34:37 +0000 (04:34 +0000)
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 <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Gary Lockyer <gary@catalyst.net.nz>
python/samba/domain/models/fields.py

index cff11661e732db746188837070c9342aeec67f0e..959f80faf5c5ae4baa32719ea598e693ced791f5 100644 (file)
@@ -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