python: domain: models: as_dict() should also exclude empty list fields
authorRob van der Linde <rob@catalyst.net.nz>
Sun, 24 Mar 2024 10:36:22 +0000 (23:36 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Thu, 28 Mar 2024 01:50:41 +0000 (01:50 +0000)
Empty list fields happen if many=True is used on the field. This means that the field is automatically initialised as an empty list, so this can only ever be sa list or None.

The side-effect of this was that it appears in as_dict() when it shouldn't, because the field isn't populated. This fixes it.

Signed-off-by: Rob van der Linde <rob@catalyst.net.nz>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
python/samba/domain/models/model.py

index 55cada972b60ab6436eb2ef1433529ac207f1628..56dacc9ddb480a3d1c7095fcf6a14b540efd4fa1 100644 (file)
@@ -193,7 +193,7 @@ class Model(metaclass=ModelMeta):
         for attr, field in self.fields.items():
             if not field.hidden or include_hidden:
                 value = getattr(self, attr)
-                if value is not None:
+                if value not in (None, []):
                     obj_dict[field.name] = value
 
         return obj_dict