Comment 4 for bug 2033632

Revision history for this message
Jacopo Rota (r00ta) wrote :

@adam we have some overrides: in particular `full_clean` is just setting the values specified in the Form and it will keep the existing at the old values.

```
class APIEditMixin:
    """A mixin that allows sane usage of Django's form machinery via the API.

    First it ensures that missing fields are not errors, then it removes
    None values from cleaned data. This means that missing fields result
    in no change instead of an error.

    :ivar submitted_data: The `data` as originally submitted.
    """

    def full_clean(self):
        """For missing fields, default to the model's existing value."""
        self.submitted_data = self.data
        self.data = get_overridden_query_dict(
            self.initial, self.data, self.fields
        )
        super().full_clean()
```

For example, patching it like

```
    def full_clean(self):
        """For missing fields, default to the model's existing value."""
        self.submitted_data = self.data
        print("DATA")
        print(self.data)
        print(self.initial)
        self.data = get_overridden_query_dict(
            self.initial, self.data, self.fields
        )
        print(self.data)
        super().full_clean()
```
and executing the test I posted above you will see

```
# FIRST DEPLOYMENT
self.data: {'enable_hw_sync': True}
self.initial: {'hostname': 'a5NPjpSsMxvbdTLVorz8', 'domain': 0, 'osystem': 'ubuntu', 'distro_series': 'ubuntu/focal', 'architecture': 'arch-xzgCJi/sub-sxIylM', 'min_hwe_kernel': None, 'hwe_kernel': None, 'swap_size': None, 'ephemeral_deploy': False, 'license_key': None, 'install_rackd': False, 'enable_hw_sync': False}
self.data: <QueryDict: {'hostname': ['a5NPjpSsMxvbdTLVorz8'], 'domain': [0], 'osystem': ['ubuntu'], 'distro_series': ['ubuntu/focal'], 'architecture': ['arch-xzgCJi/sub-sxIylM'], 'min_hwe_kernel': [None], 'hwe_kernel': [None], 'swap_size': [None], 'ephemeral_deploy': [False], 'license_key': [None], 'install_rackd': [False], 'enable_hw_sync': [True]}>

# SECOND DEPLOYMENT

self.data: {}
self.initial: {'hostname': 'a5NPjpSsMxvbdTLVorz8', 'domain': 0, 'osystem': 'ubuntu', 'distro_series': 'ubuntu/focal', 'architecture': 'arch-xzgCJi/sub-sxIylM', 'min_hwe_kernel': '', 'hwe_kernel': 'hwe-20.04', 'swap_size': None, 'ephemeral_deploy': False, 'license_key': '', 'install_rackd': False, 'enable_hw_sync': True}
self.data: <QueryDict: {'hostname': ['a5NPjpSsMxvbdTLVorz8'], 'domain': [0], 'osystem': ['ubuntu'], 'distro_series': ['ubuntu/focal'], 'architecture': ['arch-xzgCJi/sub-sxIylM'], 'min_hwe_kernel': [''], 'hwe_kernel': ['hwe-20.04'], 'swap_size': [None], 'ephemeral_deploy': [False], 'license_key': [''], 'install_rackd': [False], 'enable_hw_sync': [True]}>
```