Skip to content

Commit

Permalink
Fix: Lazy-initialize storage class to prevent unwanted migration gene…
Browse files Browse the repository at this point in the history
…ration
  • Loading branch information
samupl authored and PetrDlouhy committed Nov 14, 2023
1 parent 4e92ed1 commit d8357e7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
14 changes: 9 additions & 5 deletions import_export_celery/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
from django.db import models


def lazy_initialize_storage_class():
# If the user has specified a custom storage backend, use it.
if getattr(settings, "IMPORT_EXPORT_CELERY_STORAGE", None):
storage_class = get_storage_class(settings.IMPORT_EXPORT_CELERY_STORAGE)
return storage_class()
return None


class ImportExportFileField(models.FileField):
def __init__(self, *args, **kwargs):
# If the user has specified a custom storage backend, use it.
if getattr(settings, "IMPORT_EXPORT_CELERY_STORAGE", None):
storage_class = get_storage_class(settings.IMPORT_EXPORT_CELERY_STORAGE)
kwargs["storage"] = storage_class()

kwargs["storage"] = lazy_initialize_storage_class
super().__init__(*args, **kwargs)
29 changes: 29 additions & 0 deletions import_export_celery/migrations/0010_auto_20231013_0904.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 3.2.18 on 2023-10-13 09:04

from django.db import migrations
import import_export_celery.fields


class Migration(migrations.Migration):

dependencies = [
('import_export_celery', '0009_alter_exportjob_options_alter_importjob_options_and_more'),
]

operations = [
migrations.AlterField(
model_name='exportjob',
name='file',
field=import_export_celery.fields.ImportExportFileField(max_length=255, storage=import_export_celery.fields.lazy_initialize_storage_class, upload_to='django-import-export-celery-export-jobs', verbose_name='exported file'),
),
migrations.AlterField(
model_name='importjob',
name='change_summary',
field=import_export_celery.fields.ImportExportFileField(blank=True, null=True, storage=import_export_celery.fields.lazy_initialize_storage_class, upload_to='django-import-export-celery-import-change-summaries', verbose_name='Summary of changes made by this import'),
),
migrations.AlterField(
model_name='importjob',
name='file',
field=import_export_celery.fields.ImportExportFileField(max_length=255, storage=import_export_celery.fields.lazy_initialize_storage_class, upload_to='django-import-export-celery-import-jobs', verbose_name='File to be imported'),
),
]

0 comments on commit d8357e7

Please sign in to comment.