Skip to content

Commit 80d33ec

Browse files
committed
fix admin
1 parent 40754ac commit 80d33ec

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

backend/proteins/admin.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,54 @@ class CameraAdmin(SpectrumOwner, VersionAdmin):
222222
ordering = ("-created",)
223223

224224

225+
class SpectrumAdminForm(forms.ModelForm):
226+
"""Custom form to handle the Spectrum.data property in admin."""
227+
228+
data = forms.CharField(
229+
widget=forms.Textarea(attrs={"rows": 10, "cols": 80}),
230+
required=False,
231+
help_text="Spectrum data as [[wavelength, value], ...] pairs",
232+
)
233+
234+
class Meta:
235+
model = Spectrum
236+
fields = [
237+
"category",
238+
"subtype",
239+
"ph",
240+
"solvent",
241+
"owner_fluor",
242+
"owner_filter",
243+
"owner_light",
244+
"owner_camera",
245+
"reference",
246+
"source",
247+
"status",
248+
"created_by",
249+
"updated_by",
250+
]
251+
252+
def __init__(self, *args, **kwargs):
253+
super().__init__(*args, **kwargs)
254+
if self.instance and self.instance.pk:
255+
# Populate the data field from the property
256+
self.fields["data"].initial = self.instance.data
257+
258+
def save(self, commit=True):
259+
instance = super().save(commit=False)
260+
# Set data via the property setter if provided
261+
data_value = self.cleaned_data.get("data")
262+
if data_value:
263+
instance.data = data_value
264+
if commit:
265+
instance.save()
266+
return instance
267+
268+
225269
@admin.register(Spectrum)
226270
class SpectrumAdmin(VersionAdmin):
227271
model = Spectrum
272+
form = SpectrumAdminForm
228273
autocomplete_fields = ["reference"]
229274
list_select_related = (
230275
"owner_fluor",

0 commit comments

Comments
 (0)