Why does Django make migrations for help_text and verbose_name changes? Why does Django make migrations for help_text and verbose_name changes? python python

Why does Django make migrations for help_text and verbose_name changes?


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 Commandfrom django.db import modelsIGNORED_ATTRS = ['verbose_name', 'help_text', 'choices']original_deconstruct = models.Field.deconstructdef new_deconstruct(self):  name, path, args, kwargs = original_deconstruct(self)  for attr in IGNORED_ATTRS:    kwargs.pop(attr, None)  return name, path, args, kwargsmodels.Field.deconstruct = new_deconstruct


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.


To avoid unnecessary migrations You can do as follows:

  1. Subclass field that causes migration
  2. Write custom deconstruct method inside that field
  3. Profit

Example:

from django.db import modelsclass CustomCharField(models.CharField):  # or any other field    def deconstruct(self):        name, path, args, kwargs = super(CustomCharField, self).deconstruct()        # exclude all fields you dont want to cause migration, my example below:        if 'help_text' in kwargs:            del kwargs['help_text']        if 'verbose_name' in kwargs:            del kwargs['verbose_name']        return name, path, args, kwargs

Hope that helps