我构建了一个 django Web 应用程序来管理医疗数据。 我有很多专业使用同一个项目。我想显示特定的界面/表单/模板...取决于用户的专业性。
我使用的是 django 1.11 python 3.6。
该应用程序运行良好。我有患者和用户。 每个用户只有一个专业(心脏病专家、外科医生......),由特定类别定义并通过外键链接到用户。
模型.py
class Specialite(Auditable): #list of specialites, and link it to user !
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
spe = models.CharField( max_length = 200, null = False,)
#define which app the specialite is linked to...
data_app = models.CharField(max_length = 200, null = False, blank = False)
def __str__(self):
return self.spe
class Patient(Auditable):
# https://stackoverflow.com/questions/3052975/django-models-avoid-duplicates
class Meta:
unique_together = ["nom", "prenom", "dob"]
MALE = "MALE"
FEMALE = "FEMALE"
SEXE = (
(MALE, 'Homme'),
(FEMALE, 'Femme'),
)
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
nom = models.CharField( max_length=50,)
nom_naissance = models.CharField( max_length=50,)
prenom = models.CharField(max_length=150,)
sexe = models.CharField(max_length = 100,
choices = SEXE,
)
目前,我有一张患者表格:名字、姓氏、出生日期、体重、高度......只有所有专业共享的一般信息。
我想根据专业创建特定的onetoone表格来显示每个专业/从业者的相关信息。 每个字段都基于 SNOMED CT分类以提高一致性和数据连接。
我想了很多办法:
- 顶部有一个巨大的模型,有很多字段,abstract = true,以及使用该模型的子模型
- 一个包含很多字段的巨大模型,并且针对每个专业特定的模板、表格来更新数据,但只要医生对数据进行不同的分析,数据就可能会发生变化...
- 最终,我认为更合适的是:针对每个专业的应用程序,其中包含应用程序内的所有字段、表单等逻辑。
所以我的选择更多的是为每个专业创建一个应用程序,通过外键或文本链接到“专业”模型(例如应用程序的名称:
data_app = models.CharField(max_length = 200, null = False, blank = False)
)。
在每个应用程序中,我都有一个模型,其中只有一个通过 OneToOne 关系链接到患者的类。
class Cardiology (Auditable):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
zkf_patient = models.OneToOneField(Patient, on_delete = models.PROTECT, null = True, blank = True)
hta = models.BooleanField()
tobacco = models.BooleanField()
.....
目前,我可以为具有内联表单集工厂的患者创建一个新条目。 但是,我想做的是显示适当的模板/CBV/Urls...根据用户的专业来编辑和显示相关信息。
例如,如果我是心脏病专家:在主页上显示与心脏病学相关的特定信息以及“患者”模型类详细信息。但如果我是一名外科医生,显示相同的页面,但包含手术的具体信息......
我现在可以做到这一点,我在模板中使用 {% include '...html' %} 来插入我想要的内容。
我正在考虑创建一个特定的标签来动态显示相关信息...
但是我不知道如何处理编辑页面等......除了创建一个带有一堆字典的脚本来创建关系之外,但这对我来说似乎是一场噩梦。
您的看法是,有没有更优雅、更“逻辑”的方式。基于每个类 View 的名称也许...
感谢您的帮助!
最佳答案
我最终得到了一个自定义标签...到目前为止效果很好:)!
#TAGS
from django import template
from django.template.loader import render_to_string #generate a string from a template
import datetime
from django.urls import reverse
from django.template.base import (
Node, Template, TemplateSyntaxError, TextNode, Variable, token_kwargs,
)
register = template.Library()
def get_activ_specialite(request, context):
app_name = request.resolver_match.app_name
if app_name == 'patient':
#get the specialite of the user
specialite = request.user.profile.specialite_active.spe
specialite = '_'.join(specialite.lower().split(' '))
return specialite
elif app_name == 'hospitalisation':
#get the specialite of the service where the patient is hospitalized !
specialite = context['hospitalisation'].specialite_referente.spe
specialite = '_'.join(specialite.lower().split(' '))
return specialite
return
@register.simple_tag(takes_context=True)
def routing_antecedents(context, destination):
if 'patient' in context:
patient = context['patient']
id = patient.id
if 'hospitalisation' in context:
hos = context['hospitalisation']
id = hos.id
request = context['request']
#1/get service référent du patient
app_name = get_activ_specialite(request, context)
#2/ redirect to urls...! Name consistency is mandatory !!!
url = str(app_name + ":" + destination)
url = reverse(str(url), args=(id,) )
# return url path !
return url
@register.simple_tag(takes_context=True)
def include_antecedents_spe(context, template_name):
request = context['request']
#1/get service référent du patient
app_name = get_activ_specialite(request, context)
template_name = template_name.replace("'","").replace('"', '')
template_name = str(str(app_name) + "/" + str(template_name))
html = render_to_string(template_name, context.flatten())
return html
关于python - 根据另一个类在 django 中动态加载特定应用程序的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56429827/