Why does Django make migrations for help_text and verbose_name changes?

后端 未结 8 2270
情深已故
情深已故 2020-12-15 15:00

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

相关标签:
8条回答
  • 2020-12-15 15:38

    As @ChillarAnand noted there is a ticket solving this issue but until now (django 1.9.1) the migrations commands were not fixed.

    The least intrusive way of fixing it is to create your own maketranslatedmigrations command in <your-project>/management/commands/maketranslatedmigrations.py as

    #coding: utf-8
    
    from django.core.management.base import BaseCommand
    from django.core.management.commands.makemigrations import Command as MakeMigrations
    
    
    class Command(MakeMigrations):
        leave_locale_alone = True
        can_import_settings = True
    
        def handle(self, *app_labels, **options):
            super(Command, self).handle(*app_labels, **options)
    

    And then you can use it exactly the same as original makemigrations.

    P.S. Don't forget to add __init__.py files everywhere on the path

    0 讨论(0)
  • 2020-12-15 15:41

    You can squash it with the previous migration, sure.

    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
    
    0 讨论(0)
  • 2020-12-15 15:41

    Stop to generate migrations files when changes verbose_name or verbose_name_plural in any of your model fields.

    In a new file like src/monkey_patching/django/db/migrations/operations/change_model_options.py add this:

    from django.db.migrations.operations import models
    
    models.AlterModelOptions.ALTER_OPTION_KEYS = [
        "base_manager_name",
        "default_manager_name",
        "get_latest_by",
        "managed",
        "ordering",
        "permissions",
        "default_permissions",
        "select_on_save",
        # "verbose_name",
        # "verbose_name_plural",
    ]
    

    Tested in django 1.11.10.

    0 讨论(0)
  • 2020-12-15 15:43

    From Django 2.X, using ugettext_lazy instead of ugettext or gettext fixes it.

    0 讨论(0)
  • 2020-12-15 15:44

    This ticket addressed the problem.

    If you have changed only help_text & django generates a new migration; then you can apply changes from latest migration to previous migration and delete the latest migration.

    Just change the help_text in the previous migration to help_text present in latest migration and delete the latest migration file. Make sure to remove corresponding *.pyc file if it is present. Otherwise an exception will be raised.

    0 讨论(0)
  • 2020-12-15 15:45

    The ticket that ChillarAnand mentions is very helpfull, but at final of changelog, I did not realize if it was fixed or not, or it was fixed in newest version of Django.

    So, due to I did not found any solution for Django 1.9.13, I added a little hack to settings.py:

    if 'makemigrations' in sys.argv:
        USE_I18N = False
        USE_L10N = False
    

    Not elegant, but it works ok.

    0 讨论(0)
提交回复
热议问题