我在多个 django 网站上工作,并且在使我的项目对客户看起来不错方面受到限制。
例如,在同一个应用程序中,我有两个模型图像和图像库。如果只有一个画廊的管理员条目和一张图像表,那就更好了。
我在多个 django 网站上工作,并且在使我的项目对客户看起来不错方面受到限制。
例如,在同一个应用程序中,我有两个模型图像和图像库。如果只有一个画廊的管理员条目和一张图像表,那就更好了。
这正是InlineModelAdmin
它的用途。采取了这样的models.py:
class Gallery(models.Model):
name = models.CharField(max_length=100)
class Image(models.Model):
image = models.ImageField()
gallery = models.ForeignKey(Gallery)
你像这样创建一个 admin.py 并且只为 Gallery 注册一个管理类:
class ImageInline(admin.TabularInline):
model = Image
class GalleryAdmin(admin.ModelAdmin):
inlines = [ImageInline]
admin.site.register(Gallery, GalleryAdmin)
感谢 Dirk 的帮助,这是我的解决方案。
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)