Package: edc-lab-results
edc-lab-resultsSimple blood result data collection format for django modelsIn this designa specimen requisition for a panel is completed first (SubjectRequisition)result is received and entered into a result formif a result is admnormal or gradable, an ActionItem is created.Building the ModelBelow we create a model class withBloodResultsModelMixin. On the class we specify thelab_paneland limit the FK the requisitions of this panel usinglimit_choices_to.# models.pyfromedc_lab.model_mixinsimportCrfWithRequisitionModelMixin,requisition_fk_optionsfromedc_lab_panels.panelsimportchemistry_panelclassBloodResultsFbc(CrfWithRequisitionModelMixin,BloodResultsModelMixin,BaseUuidModel,):lab_panel=fbc_panelrequisition=models.ForeignKey(limit_choices_to={"panel__name":fbc_panel.name},**requisition_fk_options)classMeta(CrfWithActionModelMixin.Meta,BaseUuidModel.Meta):verbose_name="Blood Result: FBC"verbose_name_plural="Blood Results: FBC"The above example has no fields for results, so let’s add some model mixins, one for each result item.# models.pyclassBloodResultsFbc(CrfWithRequisitionModelMixin,HaemoglobinModelMixin,HctModelMixin,RbcModelMixin,WbcModelMixin,PlateletsModelMixin,MchModelMixin,MchcModelMixin,McvModelMixin,BloodResultsModelMixin,CrfStatusModelMixin,BaseUuidModel,):lab_panel=fbc_panelrequisition=models.ForeignKey(limit_choices_to={"panel__name":fbc_panel.name},**requisition_fk_options)classMeta(CrfWithActionModelMixin.Meta,BaseUuidModel.Meta):verbose_name="Blood Result: FBC"verbose_name_plural="Blood Results: FBC"If anActionItemis to be created because of an abnormal or reportable result item, add the ActionItem.# models.pyclassBloodResultsFbc(CrfWithActionModelMixin,CrfWithRequisitionModelMixin,HaemoglobinModelMixin,HctModelMixin,RbcModelMixin,WbcModelMixin,PlateletsModelMixin,MchModelMixin,MchcModelMixin,McvModelMixin,BloodResultsModelMixin,CrfStatusModelMixin,BaseUuidModel,):action_name=BLOOD_RESULTS_FBC_ACTIONlab_panel=fbc_panelrequisition=models.ForeignKey(limit_choices_to={"panel__name":fbc_panel.name},**requisition_fk_options)classMeta(CrfWithActionModelMixin.Meta,BaseUuidModel.Meta):verbose_name="Blood Result: FBC"verbose_name_plural="Blood Results: FBC"Building the ModeForm classThe ModelForm class just needs the Model class and the panel. In this caseBloodResultsFbcandfbc_panel.# forms.pyclassBloodResultsFbcFormValidator(BloodResultsFormValidatorMixin,CrfFormValidator):panel=fbc_panelclassBloodResultsFbcForm(ActionItemCrfFormMixin,CrfModelFormMixin,forms.ModelForm):form_validator_cls=BloodResultsFbcFormValidatorclassMeta(ActionItemCrfFormMixin.Meta):model=BloodResultsFbcfields="__all__"Building the ModelAdmin classThe ModelAdmin class needs the Model class, ModelForm class and the panel.# admin.py@admin.register(BloodResultsFbc,site=intecomm_subject_admin)classBloodResultsFbcAdmin(BloodResultsModelAdminMixin,CrfModelAdmin):form=BloodResultsFbcFormfieldsets=BloodResultFieldset(BloodResultsFbc.lab_panel,model_cls=BloodResultsFbc,extra_fieldsets=[(-1,action_fieldset_tuple)],).fieldsetsThe SubjectRequistion ModelAdmin classWhen usingautocompletefor the subject requsition FK on the result form ModelAdmin class, the subject requsition model admin class needs to filter the search results passed to the autocomplete control.If all result models are prefixed with “bloodresult”, you can filter on the path name like this:# admin.py@admin.register(SubjectRequisition,site=intecomm_subject_admin)classSubjectRequisitionAdmin(RequisitionAdminMixin,CrfModelAdmin):form=SubjectRequisitionForm# ...defget_search_results(self,request,queryset,search_term):queryset,use_distinct=super().get_search_results(request,queryset,search_term)path=urlsplit(request.META.get("HTTP_REFERER")).pathquery=urlsplit(request.META.get("HTTP_REFERER")).queryif"bloodresult"instr(path):attrs=parse_qs(str(query))try:subject_visit=attrs.get("subject_visit")[0]except(TypeError,IndexError):passelse:queryset=queryset.filter(subject_visit=subject_visit,is_drawn=YES)returnqueryset,use_distinct