gpt4 book ai didi

python - Django:如何过滤某个科室的患者?

转载 作者:行者123 更新时间:2023-12-01 03:54:39 24 4
gpt4 key购买 nike

我使用的是 django v1.8。

我扩展了现有的用户模型。

models.py

class Institution(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
department = models.CharField(max_length=100)

class Demographic(models.Model):
patient_id = models.IntegerField(unique= True ,primary_key=True)
pub_date = models.DateTimeField(auto_now=True)
author = models.ForeignKey(User)
history = HistoricalRecords()

def __str__(self):
return str(self.patient_id)

我有两个属于同一部门的用户doctornurse,我希望他们都能够访问相同的患者。

在模板中,我使用搜索引擎来搜索患者。

views.py中我有这个过滤器:

patient = Demographic.objects.filter(patient_id__icontains=id)

但这 2 个用户只能在各自部门的患者之间进行搜索。

如何在过滤器中实现此目的?我应该包含什么参数?

最佳答案

A user belongs to a department when he inserts a patient as author then this patient belongs to that department.

因此,让我们首先获取给定用户部门:

user = request.user # the doctor, the nurse etc
department = Institution.objects.get(user=user).department

现在您想要所有患者(人口统计实例),其中author.institution.department = 部门

patients = Demographic.objects.filter(author__institution__department=department)

关于python - Django:如何过滤某个科室的患者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37679702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com