How to prevent Django from generating migrations when using dynamic GoogleCloudStorage in a FileField?
We’re working on a Django project that stores video files in Google Cloud Storage using a FileField
.
In our model, we define a default bucket storage like this:
from storages.backends.gcloud import GoogleCloudStorage
from django.conf import settings
DEFAULT_STORAGE = GoogleCloudStorage(bucket_name=settings.DEFAULT_GCS_BUCKET)
class Recording(models.Model):
raw_file_gcp = models.FileField(blank=True, null=True, storage=DEFAULT_STORAGE)
However, in some parts of the system, we move files between two different GCS buckets:
- One for regular usage (e.g.
default-bucket
) - Another for retention or archival purposes (e.g.
retention-bucket
)
To do that, we dynamically update the .name
attribute of the file based on logic in the backend:
recording.raw_file_gcp.name = path_with_retention_bucket
recording.save(update_fields=["raw_file_gcp", "updated_at"])
Because the underlying storage
class contains a bucket name, every time we run makemigrations
, Django detects a change and adds a migration like this:
migrations.AlterField(
model_name='recording',
name='raw_file_gcp',
field=models.FileField(blank=True, null=True, storage=myapp.models.CustomStorage(bucket_name='default-bucket')),
)
But nothing has actually changed in the model.
To avoid these unnecessary AlterField
migrations, we implemented a custom storage class using @deconstructible
and __eq__
:
from django.utils.deconstruct import deconstructible
from storages.backends.gcloud import GoogleCloudStorage
@deconstructible
class NeutralGCSStorage(GoogleCloudStorage):
def __eq__(self, other):
return isinstance(other, NeutralGCSStorage)
And then used:
DEFAULT_STORAGE = NeutralGCSStorage(bucket_name=settings.DEFAULT_GCS_BUCKET)
But Django still generates the same migration, and doesn’t treat the storage as unchanged.
❓ What we’re looking for
How can we prevent Django from generating migrations when the only difference is the dynamic storage bucket used in a FileField
?
✅ Context
- Django 5.x
django-storages
with GoogleCloudStorage backendmakemigrations
is being run normally in CI and local dev- The only field triggering these migrations is a
FileField
with dynamic bucket logic