추가 =
class GalleryImageInline(admin.TabularInline):
"""
Gallery Image inline
"""
fieldsets = (
(
None,
{
'fields': ('name', 'image',)
}
),
)
model = Image
extra = 0
class GalleryAdmin(admin.ModelAdmin):
"""
Case Study Admin
"""
fieldsets = (
(
None,
{
'fields': ('location', 'date',)
}
),
)
inlines = (GalleryImageInline,)
list_display = ['location', 'date']
list_filter = ['location', ]
그리고 내 의견으로는 0, 당신은 실제로 필요 이 몇 가지 resizey 물건을한다는 것을 당신이 좋아하지 않을 수 있습니다 - 그래서 당신은해야 할 수도 있습니다
from django.contrib.admin.widgets import AdminFileWidget
from django.utils.translation import ugettext as _
from django.utils.safestring import mark_safe
import os
import Image
class AdminImageWidget(AdminFileWidget):
def render(self, name, value, attrs=None):
output = []
if value and getattr(value, "url", None):
image_url = value.url
file_name=str(value)
# defining the size
size='100x100'
x, y = [int(x) for x in size.split('x')]
try :
# defining the filename and the miniature filename
filehead, filetail = os.path.split(value.path)
basename, format = os.path.splitext(filetail)
miniature = basename + '_' + size + format
filename = value.path
miniature_filename = os.path.join(filehead, miniature)
filehead, filetail = os.path.split(value.url)
miniature_url = filehead + '/' + miniature
# make sure that the thumbnail is a version of the current original sized image
if os.path.exists(miniature_filename) and os.path.getmtime(filename) > os.path.getmtime(miniature_filename):
os.unlink(miniature_filename)
# if the image wasn't already resized, resize it
if not os.path.exists(miniature_filename):
image = Image.open(filename)
image.thumbnail([x, y], Image.ANTIALIAS)
try:
image.save(miniature_filename, image.format, quality=100, optimize=1)
except:
image.save(miniature_filename, image.format, quality=100)
output.append(u' <div><a href="%s" target="_blank"><img src="%s" alt="%s" /></a></div> %s ' % \
(miniature_url, miniature_url, miniature_filename, _('Change:')))
except:
pass
output.append(super(AdminFileWidget, self).render(name, value, attrs))
return mark_safe(u''.join(output))
참고 : 이미지를 인라인으로 표시 (그래서 사용자 정의 이미지 위젯을 무시) 위젯의 엄지 크기 조정 부분을 직접 재 작성하십시오.
그래서 당신은 당신 인라인의 형태로 위젯을 오버라이드 (override) 할 필요가 있기 위하여려고하고있다 :이 같은 somethig에 끝낼
class GalleryImageForm(forms.ModelForm):
"""
Image Admin Form
"""
class Meta:
model = Image
widgets = {
'image' : AdminImageWidget,
}
class GalleryImageInline(admin.TabularInline):
...
form = GalleryImageForm
...
(이 다른 프로젝트의 일부이며 추가 잔뜩있다) : 당신이 sorl-thumbnails를 사용하는 경우 경우