2013-02-09 63 views
0

我正在使用多个Django网站,并且在限制客户使我的项目看起来不错。如何在单个django模型条目中包含数据行?

例如,在同一个应用程序中,我有两个模型图像和图像库。只有画廊的管理员条目以及一张图像表格会更好。

+0

你喜欢你的画廊有图像的内联表吗? – catherine 2013-02-09 12:13:06

回答

2

这正是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,只注册一个管理类的画廊:

class ImageInline(admin.TabularInline): 
    model = Image 

class GalleryAdmin(admin.ModelAdmin): 
    inlines = [ImageInline] 

admin.site.register(Gallery, GalleryAdmin) 
+0

谢谢Eschler,这正是我想要的。 – 2013-02-11 05:11:48

0

这是我的解决方案感谢德克的帮助。

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) 
相关问题