#models.py
class MyImage(model.Model):
image = models.ImageField('my image', default=None)
#here you need to specify what model this images will be related to(which model you want to add several images with ImageField) lets say it is model "MyModel"
my_model = models.ForeignKey(MyModel,
on_delete=models.CASCADE,
related_name='images')
def preview(self):
# this is needed to see model as actually image in admin
full_url = str(self.image.url)
return format_html('<img src="{}", width=200, height=200>', full_url)
字符串 然后你需要在admin中将其添加为inline:
#admin.py
from .models import Image, MyModel
from django.contrib import admin
class ImageInline(admin.TabularInline):
model = Image
extra = 1
fields = ('image', 'preview')
readonly_fields = ('preview', )
@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
inlines = [ImageInline]
admin.site.register(Image)
1条答案
按热度按时间qgzx9mmu1#
ImageField只能存储一个图像。你能做什么:
为图像创建单独的模型:
字符串
然后你需要在admin中将其添加为inline:
型