netbox-community / netbox

The premier source of truth powering network automation. Open source under Apache 2. Try NetBox Cloud free: https://netboxlabs.com/free-netbox-cloud/
http://netboxlabs.com/oss/netbox/
Apache License 2.0
16.15k stars 2.59k forks source link

Cloning boolean custom fields does not work if their value is `False` #17096

Open peteeckel opened 3 months ago

peteeckel commented 3 months ago

Deployment Type

Self-hosted

NetBox Version

v4.0.8

Python Version

3.11

Steps to Reproduce

  1. Create a custom field 'test' with type 'Boolean', 'Is clonable' checked for an arbitrary object type, say ipam.IPAddress
  2. Set the custom field to False for an arbitrary IPAddress object and click on 'Save'
  3. Click on 'Clone'

Expected Behavior

The cloned object has its 'test' CF set to False

Observed Behavior

The cloned object has its 'test' CF set to None

If you look at the parameters of the URL for the 'Clone' button, the relevant part is test=. This is identical whether the CF 'test' was False for the original object or if it was undefined.

This is particularly relevant for required fields, as it makes it necessary to set the field explicitly for cloned objects even if cloning the field was enabled.

peteeckel commented 3 months ago

I did not run the NetBox test suite yet, but there seems to be a trivial fix:

(netbox) [root@dns netbox]# git diff 
diff --git a/netbox/utilities/querydict.py b/netbox/utilities/querydict.py
index 78395758a..dbe2e4281 100644
--- a/netbox/utilities/querydict.py
+++ b/netbox/utilities/querydict.py
@@ -55,7 +55,7 @@ def prepare_cloned_fields(instance):
     for key, value in attrs.items():
         if type(value) in (list, tuple):
             params.extend([(key, v) for v in value])
-        elif value is not False and value is not None:
+        elif value is not None:
             params.append((key, value))
         else:
             params.append((key, ''))
arthanson commented 3 months ago

@peteeckel do you want to take this one?

peteeckel commented 3 months ago

@peteeckel do you want to take this one?

Sure, I'll be glad to.