-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
17096 Do not omit "False" values from the request URL for cloned fields #17113
base: develop
Are you sure you want to change the base?
17096 Do not omit "False" values from the request URL for cloned fields #17113
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you tested this change using other models? A VRF, for instance, will not have its enforce_unique
field cloned properly because the string "False" is interpreted as true.
I ran the NetBox test suite in the assumption that would secure the fix doesn't break anything. Obviously wrong ... Given the fact that there is either the CF problem or the problem with cloning the I can have a look at why the VRF code interprets "False" as |
@jeremystretch I had a look at the problem and there is a solution, but probably not a perfect one. The interpretation of the value from the URL parameters is a Django utility function, and is applied to all boolean fields on initialisation. This results in the problem you mentioned. Custom boolean fields are not affected by that, because they aren't proper model fields after all, so it's safe to assume that all URL parameters starting with However, I'm not sure what what will happen if someone choses to use fields of type |
netbox/utilities/querydict.py
Outdated
@@ -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 and (key.startswith('cf_') or value is not False): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm afraid this isn't a tenable change: We cannot make arbitrary exceptions for certain types of fields. Instead, the form initialization logic may need to be updated to acknowledge explicit False (as opposed to undefined) values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with updating the form initialisation logic is that we can't do that indiscriminately either. There may well be a CharField
that receives the string "False"
, in which case it isn't a very useful idea to modify it to the boolean value False
.
So even when we modify the initialisation logic we still need to discriminate between field types that receive the data. Which is what I did now, this time on field type level.
350464e
to
2baee77
Compare
2baee77
to
ce7343c
Compare
This PR has been automatically marked as stale because it has not had recent activity. It will be closed automatically if no further action is taken. |
@jeremystretch Did you notice that I made some changes to this PR - it's now pending closure and the bug is really annoying ... |
@@ -31,6 +32,15 @@ class NetBoxModelForm(CheckLastUpdatedMixin, CustomFieldsMixin, TagsMixin, forms | |||
""" | |||
fieldsets = () | |||
|
|||
def __init__(self, *args, **kwargs): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While the logic makes sense, I don't think NetBoxModelForm is the best place to address this. We want to ensure that this behavior is consistent for all forms across the application, and many forms don't inherit from this class.
Maybe normalize_querydict()
would be a more appropriate place for this logic. We lose access to the individual form fields but it's probably reasonably safe to assume the string "False" implies the boolean value False
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's probably reasonably safe to assume the string "False" implies the boolean value False.
While this may be true in most cases I would not implement code that relies on this assumption. It's just that, an assumption, after all, and errors resulting from applications actually relying on "False" being a string will be pretty hard to debug. That kind of reasoning is exactly where the referenced issue came from, and the solution of just assuming "False" will always refer to a boolean value will only hide the issue better.
The question is: Are there any forms not derived from NetBoxModelForm
that are used in cloning objects?
Fixes: #17096
The current implementation leaves the URL parameters for boolean fields empty if their value is actually
False
, which leads to fields withFalse
values not being cloned correctly.