From 0b88d0509434ad319a78faf692bb4235206c9d7d Mon Sep 17 00:00:00 2001 From: igor_nadein Date: Sat, 28 Feb 2026 08:34:50 +0300 Subject: [PATCH] =?UTF-8?q?fix(documents):=20=D0=98=D1=81=D0=BF=D1=80?= =?UTF-8?q?=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20polymorphic=5Fctyp?= =?UTF-8?q?e=5Fid=20=D0=B4=D0=BB=D1=8F=20filer.File?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Создана management команда fix_filer_polymorphic - Использует прямой SQL для обновления polymorphic_ctype_id - Исправляет ошибку PolymorphicTypeUndefined при GET /api/v1/documents/ - Обработано 22 файла в базе данных --- .../commands/fix_filer_polymorphic.py | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 backend/documents/management/commands/fix_filer_polymorphic.py diff --git a/backend/documents/management/commands/fix_filer_polymorphic.py b/backend/documents/management/commands/fix_filer_polymorphic.py new file mode 100644 index 00000000..de0b633b --- /dev/null +++ b/backend/documents/management/commands/fix_filer_polymorphic.py @@ -0,0 +1,61 @@ +""" +Management command для исправления polymorphic_ctype_id в django-filer File объектах +""" +from django.core.management.base import BaseCommand +from django.contrib.contenttypes.models import ContentType +from django.db import connection +from filer.models import File, Image + + +class Command(BaseCommand): + help = 'Исправляет polymorphic_ctype_id для всех filer.File объектов' + + def handle(self, *args, **options): + self.stdout.write('Исправление polymorphic_ctype_id для filer.File объектов...') + + # Получаем ContentType для базового File класса + file_ctype = ContentType.objects.get_for_model(File) + image_ctype = ContentType.objects.get_for_model(Image) + + # Используем прямой SQL чтобы избежать проблем с polymorphic + with connection.cursor() as cursor: + # Проверяем сколько файлов без polymorphic_ctype_id + cursor.execute( + "SELECT COUNT(*) FROM filer_file WHERE polymorphic_ctype_id IS NULL" + ) + count = cursor.fetchone()[0] + + if count == 0: + self.stdout.write(self.style.SUCCESS('Все файлы уже имеют правильный polymorphic_ctype_id')) + return + + self.stdout.write(f'Найдено {count} файлов без polymorphic_ctype_id') + + # Обновляем файлы с типом image/* + cursor.execute( + """ + UPDATE filer_file + SET polymorphic_ctype_id = %s + WHERE polymorphic_ctype_id IS NULL + AND mime_type LIKE 'image/%%' + """, + [image_ctype.id] + ) + image_fixed = cursor.rowcount + + # Обновляем остальные файлы + cursor.execute( + """ + UPDATE filer_file + SET polymorphic_ctype_id = %s + WHERE polymorphic_ctype_id IS NULL + """, + [file_ctype.id] + ) + file_fixed = cursor.rowcount + + total_fixed = image_fixed + file_fixed + self.stdout.write(self.style.SUCCESS( + f'Успешно исправлено {total_fixed} файлов ' + f'({image_fixed} изображений, {file_fixed} остальных)' + ))