V
V
Vampre2019-12-26 15:58:54
linux
Vampre, 2019-12-26 15:58:54

Why is there no rights to read images when uploading?

For blog post images, the following model is used:

# models.py
class PostBodyImage(models.Model):
    image = models.ImageField("Изображение 960x570px", upload_to=get_post_body_media_path)
    descript = models.CharField("Описание", max_length=100)
    post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='body_images')

# admin.py
class BodyImagesInlines(admin.TabularInline):
    form = PostBodyImageForm
    model = PostBodyImage
    fields = ('get_preview', 'image', 'descript')
    readonly_fields = ('get_preview',)

    def get_preview(self, obj):
        if not obj.image:
            return ''
        im = get_thumbnail(obj.image, '168x100', crop='center', quality=99)
        return mark_safe('<img src="{}">'.format(im.url))
    get_preview.short_description = 'Превью'


class PostAdmin(admin.ModelAdmin):
    form = PostImageControlForm
    fields = ('count', 'status', 'get_preview_url', 'image', 'head_image', 'title', 'descript', 'body', 'main_post', 'category')
    readonly_fields = ('count', 'get_preview_url',)
    formfield_overrides = {
        models.TextField: {'widget': forms.Textarea(attrs={'rows': '4', 'cols': '50'})}
    }
    list_display = ('title', 'get_category', 'main_post', 'count', 'status')
    list_editable = ('main_post', 'status')
    list_filter = ('category', 'main_post', 'status')
    inlines = [
       BodyImagesInlines, 
    ]

    def get_fields(self, request, obj=None):
        """Исключить поле превью из страницы создания поста"""
        fields = list(super(PostAdmin, self).get_fields(request, obj))
        if obj is None:
            fields.remove('get_preview_url')
        return fields

    def get_category(self, obj):
        return ',\n'.join([category.title for category in obj.category.all()])

    def get_preview_url(self, obj):
        return mark_safe('<a target="_blank" href="{}">{}</a>'.format(obj.get_preview_url(), obj.get_preview_url()))
    get_preview_url.short_description = 'Превью'

admin.site.register(Post, PostAdmin)

The problem is that if I add one image at a time, everything is OK, but if I add several images at once (to the inline), then they are saved without read rights, respectively, they are not loaded onto the page ...
5e04ae6550e6b066456792.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question