如何在单个django模型条目中包含数据行?
我在多个django网站上工作,在让我的项目看起来更适合客户方面受到了限制 例如,在同一个应用程序中,我有两个模型图像和图像库。如果有一个画廊的管理员条目和一张图片表就更好了。这正是它的用途。以models.py为例,如下所示:如何在单个django模型条目中包含数据行?,django,django-admin,Django,Django Admin,我在多个django网站上工作,在让我的项目看起来更适合客户方面受到了限制 例如,在同一个应用程序中,我有两个模型图像和图像库。如果有一个画廊的管理员条目和一张图片表就更好了。这正是它的用途。以models.py为例,如下所示: class Gallery(models.Model): name = models.CharField(max_length=100) class Image(models.Model): image = models.ImageField() g
class Gallery(models.Model):
name = models.CharField(max_length=100)
class Image(models.Model):
image = models.ImageField()
gallery = models.ForeignKey(Gallery)
您可以像这样创建admin.py,并且只为库注册admin类:
class ImageInline(admin.TabularInline):
model = Image
class GalleryAdmin(admin.ModelAdmin):
inlines = [ImageInline]
admin.site.register(Gallery, GalleryAdmin)
多亏了德克的帮助,这就是我的解决方案
from django.db import models
PHOTO_PATH = 'media_gallery'
class Gallerys(models.Model):
title = models.CharField(max_length=30, help_text='Title of the image maximum 30 characters.')
slug = models.SlugField(unique_for_date='date', help_text='This is automatic, used in the URL.')
date = models.DateTimeField()
class Meta:
verbose_name_plural = "Image Galleries"
ordering = ('-date',)
def __unicode__(self):
return self.title
class Images(models.Model):
title = models.CharField(max_length=30, help_text='Title of the image maximum 30 characters.')
content = models.FileField(upload_to=PHOTO_PATH,blank=False, help_text='Ensure the image size is small and it\'s aspect ratio is 16:9.')
gallery = models.ForeignKey(Gallerys)
date = models.DateTimeField()
class Meta:
verbose_name_plural = "Images"
ordering = ('-date',)
def __unicode__(self):
return self.title
import models
from django.contrib import admin
class ImageInline(admin.TabularInline):
model = Images
class GallerysAdmin(admin.ModelAdmin):
list_display = ('title', 'date', 'slug')
inlines = [ImageInline]
admin.site.register(models.Gallerys,GallerysAdmin)
你喜欢你的画廊有图像内嵌表吗?谢谢Eschler,这正是我想要的。