From 7a16782ebf894446eeaef95c8a0558ed49ecde58 Mon Sep 17 00:00:00 2001 From: Jeff Bradberry Date: Wed, 9 Jun 2021 11:50:35 -0400 Subject: [PATCH] Fix a problem with using PrimaryKeyRelatedField in our settings registry DRF, when using this field, short-circuits the call to .to_representation() when the value is None, since clearly you aren't going to be able to get the .pk attribute off of it in that case. We were previously unconditionally calling .to_representation() which throws an error when we try to clear the value of DEFAULT_EXECUTION_ENVIRONMENT. --- awx/conf/settings.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/awx/conf/settings.py b/awx/conf/settings.py index fc3e228cb4..7e7abeac54 100644 --- a/awx/conf/settings.py +++ b/awx/conf/settings.py @@ -23,6 +23,7 @@ import cachetools # AWX from awx.main.utils import encrypt_field, decrypt_field from awx.conf import settings_registry +from awx.conf.fields import PrimaryKeyRelatedField from awx.conf.models import Setting from awx.conf.migrations._reencrypt import decrypt_field as old_decrypt_field @@ -420,9 +421,9 @@ class SettingsWrapper(UserSettingsHolder): raise ImproperlyConfigured('Setting "{}" is read only.'.format(name)) try: - data = field.to_representation(value) + data = None if value is None and isinstance(field, PrimaryKeyRelatedField) else field.to_representation(value) setting_value = field.run_validation(data) - db_value = field.to_representation(setting_value) + db_value = None if setting_value is None and isinstance(field, PrimaryKeyRelatedField) else field.to_representation(setting_value) except Exception as e: logger.exception('Unable to assign value "%r" to setting "%s".', value, name, exc_info=True) raise e