Opened 2 years ago

Closed 22 months ago

Last modified 22 months ago

#34192 closed Bug (fixed)

Callable storage on FileField fails to deconstruct when it returns default_storage

Reported by: Matt Westcott Owned by: Matt Westcott
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If the storage argument on a FileField is set to a callable that returns default_storage, it is omitted from the deconstructed form of the field, rather than being included as a reference to the callable as expected.

For example, given a model definition:

from django.core.files.storage import FileSystemStorage, default_storage
from django.db import models
import random


other_storage = FileSystemStorage(location='/media/other')


def get_storage():
    return random.choice([default_storage, other_storage])


class MyModel(models.Model):
    my_file = models.FileField(storage=get_storage)

repeatedly running makemigrations will randomly generate a migration that alternately includes or omits storage=myapp.models.get_storage on the FileField definition.

This case was overlooked in the fix for #31941 - the deconstruct method tests if self.storage is not default_storage to determine whether to add the storage kwarg, but at this point self.storage is the evaluated version, so it wrongly returns false for a callable that returns default_storage.

Change History (10)

comment:1 by Carlton Gibson, 2 years ago

Triage Stage: UnreviewedAccepted

Yes, OK. That looks correct. We're essentially saying that when hasattr(self, "_storage_callable") that should unconditionally be used by deconstruct. 🤔

comment:2 by Mariusz Felisiak, 2 years ago

Component: MigrationsDatabase layer (models, ORM)

We should probably use getattr(self, "_storage_callable", self.storage) in both lines.

comment:3 by Francesco Panico, 2 years ago

Owner: changed from nobody to Francesco Panico
Status: newassigned

comment:4 by Abhinav Yadav, 23 months ago

Owner: Francesco Panico removed

comment:5 by Abhinav Yadav, 23 months ago

Owner: set to Abhinav Yadav

comment:6 by Matt Westcott, 22 months ago

Owner: changed from Abhinav Yadav to Matt Westcott

comment:8 by Mariusz Felisiak, 22 months ago

Triage Stage: AcceptedReady for checkin

comment:9 by Mariusz Felisiak <felisiak.mariusz@…>, 22 months ago

Resolution: fixed
Status: assignedclosed

In ef85b6b:

Fixed #34192 -- Preserved callable storage when it returns default_storage.

comment:10 by Mariusz Felisiak <felisiak.mariusz@…>, 22 months ago

In b332a96:

[4.2.x] Fixed #34192 -- Preserved callable storage when it returns default_storage.

Backport of ef85b6bf0bc5a8b194f0724cf5bbedbcee402b96 from main

Note: See TracTickets for help on using tickets.
Back to Top