在父模型中显示外键数据

时间:2012-02-24 02:06:59

标签: django django-models django-admin

我有2个型号:

class Sideshow_manager(models.Model):
    url_id              = models.CharField(max_length=250,editable=False,unique=True)
    title               = models.CharField(max_length=250)
    date                = models.DateField()
    image               = models.ImageField(upload_to='slideshow',verbose_name='400*400px')
    image_title         = models.CharField(max_length=100)
    image_alt           = models.CharField(max_length=100)    
    page_title          = models.CharField(max_length=250)
    page_description    = models.CharField(max_length=250)

class Slideshow_image_manager(models.Model):
    url_id              = models.CharField(max_length=250,editable=False,unique=True)
    parent              = models.ForeignKey(Sideshow_manager)
    image               = models.ImageField(upload_to='slideshow',verbose_name='400*400px')
    image_title         = models.CharField(max_length=100)
    image_alt           = models.CharField(max_length=100)    
    page_title          = models.CharField(max_length=250)
    page_description    = models.CharField(max_length=250)

我可以让Django管理员显示Slideshow_image_manager的所有记录,当我转到Sideshow_manager的一个项目时,Slideshow_image_manager采用了父级而不是显示2个列表分开吗?

1 个答案:

答案 0 :(得分:0)

您可以使用内联,即。

class SlideshowImageManagerInline(admin.TabularInline):
  model=Slideshow_image_manager
  ...

class SideshowManagerAdmin(admin.ModelAdmin):
  inlindes=[SlideshowImageManagerInline]
  ...

admin.site.register(Sideshow_manager, SideshowManagerAdmin)

文档为here