File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed
Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff 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 )
226270class SpectrumAdmin (VersionAdmin ):
227271 model = Spectrum
272+ form = SpectrumAdminForm
228273 autocomplete_fields = ["reference" ]
229274 list_select_related = (
230275 "owner_fluor" ,
You can’t perform that action at this time.
0 commit comments