Why does Django make migrations for help_text and verbose_name changes?

后端 未结 8 2277
情深已故
情深已故 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:50

    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 models
    
    class 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

提交回复
热议问题