Skip to content

Commit

Permalink
fix: ensure identifier field handles auto-generated values
Browse files Browse the repository at this point in the history
Refactored the `identifier` field to be optional and handle cases where it relies on auto-generated values. Added logic to clean the `identifier` field by checking for a provided initial value in `self.data`. This ensures robust handling of the field without relying on user input.
  • Loading branch information
tymees committed Jan 29, 2025
1 parent a3eade6 commit 2ca3453
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions humitifier-server/src/hosts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,21 @@ class Meta:
"source_type": forms.RadioSelect,
"identifier": forms.TextInput(attrs={"disabled": True}),
}

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

# It's a disabled field, so it won't be sent to the server on submission
# We're relying on auto-generated values anyway
self.fields["identifier"].required = False

def clean_identifier(self):
# Should not happen?
if "identifier" in self.cleaned_data and self.cleaned_data["identifier"]:
return self.cleaned_data["identifier"]

# Get the initial auto-generated value from data
if "initial-identifier" in self.data:
return self.data["initial-identifier"]

return None

0 comments on commit 2ca3453

Please sign in to comment.