django外键

时间:2013-03-08 13:41:42

标签: django

我的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)

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

1 个答案:

答案 0 :(得分:1)

您使用list_displaylist_filter所做的事情与管理界面中列出的列出LabName对象的列表相关。

假设一个LabName具有一对多ComponentDescription个实体,则需要Django的InlineModelAdmin来显示属于ComponentDescription的{​​{1}}个对象的列表特定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) 是通用TabularInline的子类。