如何限制 django 中相关对象的选择?

发布于 2024-12-12 06:16:03 字数 722 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看轻我的陪伴 2024-12-19 06:16:03

两个示例仅略有不同:

第一:

class ScanInlineAdmin(admin.TabularAdmin):
    model = Scan
    formset = ScanInlineFormset

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == "visit":
            patient = self.get_object(kwargs['request'], Patient)
            kwargs["queryset"] = Visit.objects.filter(patient=patient)
        return super(ScanInlineAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)   

    def get_object(self, request, model):
        object_id = request.META['PATH_INFO'].strip('/').split('/')[-1]
        try:
            object_id = int(object_id)
        except ValueError:
            return None
        return model.objects.get(pk=object_id)

第二:

class ScanInline(admin.TabularInline):
    model = Scan
    formset = ScanInlineFormset

    def formfield_for_dbfield(self, field, **kwargs):
        if field.name == 'visit':
            # Note - get_object hasn't been defined yet
            patient = self.get_object(kwargs['request'], Patient)
            vists = Visit.objects.filter(patient=patient)
            return forms.ModelChoiceField(queryset=visits)
        return super(ScanInline, self).formfield_for_dbfield(field, **kwargs)

    def get_object(self, request, model):
        object_id = request.META['PATH_INFO'].strip('/').split('/')[-1]
        try:
            object_id = int(object_id)
        except ValueError:
            return None
        return model.objects.get(pk=object_id)

您可能会发现这个 文章有帮助。我假设您在患者页面管理中编辑/查看要输入新扫描的位置。这是正确的吗?

Two examples only slightly different:

First:

class ScanInlineAdmin(admin.TabularAdmin):
    model = Scan
    formset = ScanInlineFormset

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == "visit":
            patient = self.get_object(kwargs['request'], Patient)
            kwargs["queryset"] = Visit.objects.filter(patient=patient)
        return super(ScanInlineAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)   

    def get_object(self, request, model):
        object_id = request.META['PATH_INFO'].strip('/').split('/')[-1]
        try:
            object_id = int(object_id)
        except ValueError:
            return None
        return model.objects.get(pk=object_id)

Second:

class ScanInline(admin.TabularInline):
    model = Scan
    formset = ScanInlineFormset

    def formfield_for_dbfield(self, field, **kwargs):
        if field.name == 'visit':
            # Note - get_object hasn't been defined yet
            patient = self.get_object(kwargs['request'], Patient)
            vists = Visit.objects.filter(patient=patient)
            return forms.ModelChoiceField(queryset=visits)
        return super(ScanInline, self).formfield_for_dbfield(field, **kwargs)

    def get_object(self, request, model):
        object_id = request.META['PATH_INFO'].strip('/').split('/')[-1]
        try:
            object_id = int(object_id)
        except ValueError:
            return None
        return model.objects.get(pk=object_id)

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?

笨死的猪 2024-12-19 06:16:03

我认为 formfield_for_foreignkey 方法会对您有所帮助。您可以在管理类中实现它。请参阅文档

I think that formfield_for_foreignkey method will be helpfull for you. You can implement it in admin class. See docs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文