为什么当我们更改FileField的存储属性时django会创建一个迁移文件,当存储类型没有存储在数据库中时?
Why does django create a migration file when we change the storage attribute of FileField, when the storage type is not stored in the database?
我不想在每次更改 FileField 的存储时都创建迁移文件。我正在从 settings.py 获取存储 class 并且它是可配置的。
settings.py
Storage = S3BotoStorage(bucket='example')
models.py
from django.conf import settings
class myModel(models.Model):
file = models.FileField(upload_to='', blank=True, storage=settings.Storage)
TLDR:这是一次空迁移,无害,让它继续阅读或尝试不同的东西可能只是浪费时间
每当您对模型进行更改时,django 都必须进行迁移,因为它需要跟踪随着时间的推移对模型所做的更改。然而,这并不总是意味着将在数据库中进行修改。这里产生的迁移是一个空迁移。您的迁移可能看起来像这样,您会说,这不是空的!!
class Migration(migrations.Migration):
dependencies = [
('Whosebug', '0010_jsonmodel'),
]
operations = [
migrations.AlterField(
model_name='jsonmodel',
name='jfield',
field=Whosebug.models.MyJsonField(),
),
migrations.AlterField(
model_name='parent',
name='picture',
field=models.ImageField(storage=b'Bada', upload_to=b'/home/'),
),
]
但确实如此!!只是做
./manage.py sqlmigrate <myapp> <migration_number>
而且你会发现它不会产生任何SQL!根据@sayse
的建议引用manual
Django will make migrations for any change to your models or fields -
even options that don’t affect the database - as the only way it can
reconstruct a field correctly is to have all the changes in the
history, and you might need those options in some data migrations
later on (for example, if you’ve set custom validators).
这是我对你的问题的解释 - 你想使用你的 settings.py 中指定的存储 class(将来可以更改)存储文件。
假设您在 settings.py 和 运行 makemigrations 中指定了 xyz 存储 class。 Django 将创建一个迁移文件,其存储属性与您在 settings.py 中指定的一样。
现在,如果您在 settings.py 中更改存储 class 并且不 运行 进行迁移并上传您的文件,您的文件将上传到您在设置文件,即使您没有 运行 makemigrations。
希望对您有所帮助。
我不想在每次更改 FileField 的存储时都创建迁移文件。我正在从 settings.py 获取存储 class 并且它是可配置的。
settings.py
Storage = S3BotoStorage(bucket='example')
models.py
from django.conf import settings
class myModel(models.Model):
file = models.FileField(upload_to='', blank=True, storage=settings.Storage)
TLDR:这是一次空迁移,无害,让它继续阅读或尝试不同的东西可能只是浪费时间
每当您对模型进行更改时,django 都必须进行迁移,因为它需要跟踪随着时间的推移对模型所做的更改。然而,这并不总是意味着将在数据库中进行修改。这里产生的迁移是一个空迁移。您的迁移可能看起来像这样,您会说,这不是空的!!
class Migration(migrations.Migration):
dependencies = [
('Whosebug', '0010_jsonmodel'),
]
operations = [
migrations.AlterField(
model_name='jsonmodel',
name='jfield',
field=Whosebug.models.MyJsonField(),
),
migrations.AlterField(
model_name='parent',
name='picture',
field=models.ImageField(storage=b'Bada', upload_to=b'/home/'),
),
]
但确实如此!!只是做
./manage.py sqlmigrate <myapp> <migration_number>
而且你会发现它不会产生任何SQL!根据@sayse
的建议引用manualDjango will make migrations for any change to your models or fields - even options that don’t affect the database - as the only way it can reconstruct a field correctly is to have all the changes in the history, and you might need those options in some data migrations later on (for example, if you’ve set custom validators).
这是我对你的问题的解释 - 你想使用你的 settings.py 中指定的存储 class(将来可以更改)存储文件。
假设您在 settings.py 和 运行 makemigrations 中指定了 xyz 存储 class。 Django 将创建一个迁移文件,其存储属性与您在 settings.py 中指定的一样。
现在,如果您在 settings.py 中更改存储 class 并且不 运行 进行迁移并上传您的文件,您的文件将上传到您在设置文件,即使您没有 运行 makemigrations。
希望对您有所帮助。