如何在Django Admin中显示关系?

rhfm7lfc  于 2022-12-01  发布在  Go
关注(0)|答案(1)|浏览(144)

我正在使用Django 2.1,我很难弄清楚如何在Admin中显示关系。我有两个Model HospitalUnit,第三个是Exam。它们的设置如下:

  • Hospital可以有多个设备,一个设备属于一家医院。
  • Exam模型具有检查名称和医院名称等检查属性。
class Hospital(models.Model):

    name = models.CharField(max_length=200)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name

class Unit(models.Model):

    unit_name = models.CharField(max_length=200)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    parent_company = models.ForeignKey(
        Hospital, related_name='unit_parent', on_delete=models.CASCADE, verbose_name="Hospital")

class Exam(models.Model):

    unit_name = models.ForeignKey(
        Hospital, related_name='hospital_pk', on_delete=models.CASCADE)
    exam_name = models.CharField(max_length=255, blank=True,
                                 null=True, verbose_name="Exam's name")
    date = models.DateField()
    time = models.TimeField()

    def __str__(self):
        return self.exam_name

class CustomExamAdmin(admin.ModelAdmin):

    model = Exam
    list_display = ('exam_name', 'time', 'alert',
                    'document', 'notes', 'report', 'patients_name', 'unit_name')
    list_filter = ('exam_name', 'time')
    list_select_related = ('unit_name',)
    fieldsets = (
        ("Hospital's Name:", {
            'fields': ('unit_name',)}),
        ('Types of Exams:', {
            'fields': ('exam_name', 'modality', 'model_used')}),
        ("Patient's name:", {
            'fields': ('patients_name',)}),
     )

因此,在Django Admin的Exams页面中,我可以选择医院的名称,但是我不能选择属于医院的单位。我需要选择医院以及属于医院的单位。我是否应该在Exam中添加另一个ForeignKey,如下所示:选择的单元=型号.外键(单元,相关名称='单元pk',删除时=型号.CASCADE)
例如:医院:布鲁克代尔大学医院单位:市区单位
我得到的结果:enter image description here

q5lcpyga

q5lcpyga1#

为了在Admin中选择Unit,首先需要将其添加到Exam模型中。您有一个名为unit_name的字段,但您将其定义为Hospital模型的外键,而不是Unit模型的外键。如果您有一个unit_name字段,则实际上并不需要单独的hospital字段。因为你可以很容易地从unit_name.parent_company得到它。所以你可以这样做:

class Exam(models.Model):

    unit_name = models.ForeignKey(
        Unit, related_name='exams', on_delete=models.CASCADE)
    exam_name = models.CharField(max_length=255, blank=True,
                                 null=True, verbose_name="Exam's name")
    date = models.DateField()
    time = models.TimeField()

    def __str__(self):
        return self.exam_name

对于管理员来说,我认为最简单的方法是在admin中选择医院名称时,将医院名称作为单元名称的一部分,而不是分别选择医院和单元,如Brookdale University Hospital|Downtown Unit。这样,您只需要一个下拉字段。您可以这样做:

from django import forms

class UnitChoiceField(forms.ModelChoiceField):
     def label_from_instance(self, obj):
         return "{}|{}".format(obj.parent_company.name, obj.unit_name)

class CustomExamAdmin(admin.ModelAdmin):

    model = Exam
    list_display = ('exam_name', 'time', 'alert',
                    'document', 'notes', 'report', 'patients_name', 'get_hospital', 'unit_name')
    list_filter = ('exam_name', 'time')
    list_select_related = ('unit_name__parent_company',)
    fieldsets = (
        ("Hospital's Name:", {
            'fields': ('unit_name',)}),
        ('Types of Exams:', {
            'fields': ('exam_name', 'modality', 'model_used')}),
        ("Patient's name:", {
            'fields': ('patients_name',)}),
     )

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == 'unit_name':
            return UnitChoiceField(
                queryset=Unit.objects.all().select_related('parent_company').order_by(
                'parent_company.name', 'unit_name'))
        return super().formfield_for_foreignkey(db_field, request, **kwargs)

    def get_hospital(self, obj):
    #  required to add hospital name to list_display
        return obj.unit_name.parent_company.name           
    get_hospital.short_description = 'Hospital'

另一个注意事项:ForeignKeyField中的related_name是从外部模型返回到您正在定义的模型的关系。

parent_company = models.ForeignKey(
    Hospital, related_name='unit_parent', on_delete=models.CASCADE, verbose_name="Hospital")

应该是

parent_company = models.ForeignKey(
    Hospital, related_name='units', on_delete=models.CASCADE, verbose_name="Hospital")

然后,从Hospital模型中,您可以将单位列表称为units

相关问题