如何限制 django 中相关对象的选择?
我正在尝试使用 django 来组织和管理我的临床研究数据。我想我只需要管理界面就可以了,因为这是一个不需要公开的幕后数据库。我会编程,但以前从未使用过Python,所以我陷入了简单的事情中。
我有多次就诊的患者。每次访问都涉及多次扫描。在管理界面中,我可以看到所有访问和所有扫描,作为每个患者页面上的内嵌内容。但是,在添加新扫描时,如何才能将与其关联的就诊选择仅限于相关患者的就诊,而不是其他患者的就诊?
模型(简化):
class Patient(models.Model):
patient_id = models.CharField(max_length=16, primary_key=True)
first_name = models.CharField(max_length=64)
last_name = models.CharField(max_length=64)
class Visit(models.Model):
patient = models.ForeignKey(Patient)
visit_date = models.DateTimeField()
class Scan(models.Model):
patient = models.ForeignKey(Patient)
visit = models.ForeignKey(Visit)
scan_type = models.CharField(max_length=32)
非常感谢您的帮助......
I am trying to use django to organize and manage my clinical research data. I think I can get by with just the admin interface, since this is a behind-the-scenes database that does not need to go public. I can program, but never in python before, so I am getting stuck on simple things.
I have Patients who make multiple Visits. Each Visit involves multiple Scans. In the admin interface, I can see all the Visits and all the Scans, as inlines on each Patient's page. However, when adding a new Scan, how can I make it so that the choice of which Visit to associate it with is limited to Visits for the Patient in question, not other Patients?
Models (simplified):
class Patient(models.Model):
patient_id = models.CharField(max_length=16, primary_key=True)
first_name = models.CharField(max_length=64)
last_name = models.CharField(max_length=64)
class Visit(models.Model):
patient = models.ForeignKey(Patient)
visit_date = models.DateTimeField()
class Scan(models.Model):
patient = models.ForeignKey(Patient)
visit = models.ForeignKey(Visit)
scan_type = models.CharField(max_length=32)
Thanks a lot for any help...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
两个示例仅略有不同:
第一:
第二:
您可能会发现这个 文章有帮助。我假设您在患者页面管理中编辑/查看要输入新扫描的位置。这是正确的吗?
Two examples only slightly different:
First:
Second:
You may find this article helpful. I assume your in the patient page admin edit/view where you want to enter the new scan. Is that correct?
我认为 formfield_for_foreignkey 方法会对您有所帮助。您可以在管理类中实现它。请参阅文档。
I think that formfield_for_foreignkey method will be helpfull for you. You can implement it in admin class. See docs.