Django为什么要为help_text和verbose_name更改进行迁移? [英] Why does Django make migrations for help_text and verbose_name changes?

查看:244
本文介绍了Django为什么要为help_text和verbose_name更改进行迁移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我为任何模型字段更改help_textverbose_name并运行python manage.py makemigrations时,它将检测到这些更改并创建一个新的迁移,例如0002_xxxx.py.

When I change help_text or verbose_name for any of my model fields and run python manage.py makemigrations, it detects these changes and creates a new migration, say, 0002_xxxx.py.

我正在使用PostgreSQL,我认为这些更改与我的数据库无关(我想知道是否存在与这些更改相关的DBMS).

I am using PostgreSQL and I think these changes are irrelevant to my database (I wonder if a DBMS for which these changes are relevant exists at all).

为什么Django会为此类更改生成迁移?是否可以忽略它们?

Why does Django generate migrations for such changes? Is there an option to ignore them?

我可以手动将对0002_xxxx.py所做的更改应用于先前的迁移(0001_initial.py),并安全删除0002_xxxx.py吗?

Can I apply the changes from 0002_xxxx.py to the previous migration (0001_initial.py) manually and safely delete 0002_xxxx.py?

是否可以自动更新以前的迁移?

Is there a way to update previous migration automatically?

推荐答案

您可以或者,如果您根本不想输出这些迁移,则可以通过将makemigrationsmigrate命令放在应用程序的management/commands/makemigrations.py中来覆盖它们:

Or if you don't want to output those migrations at all, you can override the makemigrations and migrate command by putting this in management/commands/makemigrations.py in your app:

from django.core.management.commands.makemigrations import Command
from django.db import models

IGNORED_ATTRS = ['verbose_name', 'help_text', 'choices']

original_deconstruct = models.Field.deconstruct

def new_deconstruct(self):
  name, path, args, kwargs = original_deconstruct(self)
  for attr in IGNORED_ATTRS:
    kwargs.pop(attr, None)
  return name, path, args, kwargs

models.Field.deconstruct = new_deconstruct

这篇关于Django为什么要为help_text和verbose_name更改进行迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆