:
# models.py
class City(models.Model):
# your fields
class CityImage(models.Model):
city = models.ForeignKey('City', related_name='images')
image = models.ImageField(upload_to=image_upload_path)
# admin.py
from django.contrib import admin
from myapp.models import City, CityImage
class CityImageInline(admin.TabularInline):
model = CityImage
class CityAdmin(admin.ModelAdmin):
inlines = [CityImageInline]
admin.site.register(City, CityAdmin)
축소에 관해서는, 당신이 당신의 City
모델 방법에 필요한 것 관련 이미지 중 원하는 이미지를 미리보기 이미지로 사용하려면 다음과 같이하십시오.
import Image
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
from django.core.files.base import ContentFile
# other imports and models
class City(models.Model):
# your fields
def get_thumbnail(self, thumb_size=None):
# find a way to choose one of the uploaded images and
# assign it to `chosen_image`.
base = Image.open(StringIO(chosen_image.image.read())) # get the image
size = thumb_size
if not thumb_size:
# set a default thumbnail size if no `thumb_size` is given
rate = 0.2 # 20% of the original size
size = base.size
size = (int(size[0] * rate), int(size[1] * rate))
base.thumbnail(size) # make the thumbnail
thumbnail = StringIO()
base.save(thumbnail, 'PNG')
thumbnail = ContentFile(thumbnail.getvalue()) # turn the tumbnail to a "savable" object
return thumbnail
이 기능이 유용 할 것입니다. :)
ImageField의 업로드 경로를 동적으로 변경할 수 있습니까? 그래서/images/country_name/state_name/city_name /에 이미지를 업로드 할 수 있습니까? – HighLife
가능합니다. upload_to는 경로를 반환하는 함수가 될 수 있습니다. 이것을 확인하십시오 : https://docs.djangoproject.com/ko/1.4/ref/models/fields/#django.db.models.FileField.upload_to – Gerard