3

我有以下内容models.py

from django.db import models

class LabName(models.Model):
    labsname=models.CharField(max_length=30)
    def __unicode__(self):
     return self.labsname

class ComponentDescription(models.Model):
       lab_Title=models.ForeignKey('Labname')
       component_Name = models.CharField(max_length=30)
       description = models.CharField(max_length=20)
        purchased_Date = models.DateField()
       status = models.CharField(max_length=30)
       to_Do = models.CharField(max_length=30,blank=True) 
       remarks = models.CharField(max_length=30)

       def __unicode__(self):
           return self.component

我有以下内容admin.py

from django.contrib import admin
from Lab_inventory.models import ComponentDescription,LabName

class ComponentDescriptionAdmin(admin.ModelAdmin):
    list_display= ('lab_Title','component_Name','description','purchased_Date','status','to_Do','remarks')          
    list_filter=('lab_Title','status','purchased_Date')

admin.site.register(LabName)
admin.site.register(ComponentDescription,ComponentDescriptionAdmin)

我想要的是显示组件描述下的字段以显示在实验室标题下(与每个实验室标题相关的字段应显示在该实验室名称下)

4

1 回答 1

1

您正在做什么list_displaylist_filter与管理屏幕中显示的列表相关,其中列出了 LabName 对象的列表。

假设一个实体LabName具有一对多的ComponentDescription实体,您需要 DjangoInlineModelAdmin来显示ComponentDescription属于LabName特定LabName实体的管理页面内的对象列表。代码将具有以下结构:

from django.contrib import admin
from Lab_inventory.models import ComponentDescription,LabName

class ComponentDescriptionInline(admin.TabularInline):
    model = ComponentDescription

class LabNameAdmin(admin.ModelAdmin):
    inlines = [
        ComponentDescriptionInline,
    ]

admin.site.register(LabName, LabNameAdmin)

whereTabularInline是泛型的子类InlineModelAdmin

于 2013-03-08T13:50:31.617 回答