Skip to content

Instantly share code, notes, and snippets.

@augustomen
Last active March 28, 2023 20:19
Show Gist options
  • Save augustomen/a4d824e68221bef166225863eeed758c to your computer and use it in GitHub Desktop.
Save augustomen/a4d824e68221bef166225863eeed758c to your computer and use it in GitHub Desktop.
Django - Prevent migration of non-database attributes

Prevent migration of non-database attributes

By default, when creating migrations, Django will compare all the attributes of all models and all fields and compare them to the latest migrated state. This means that changes to attributes like verbose_name, choices, help_text, and validators cause new migrations to be created - even though they have no effect on the underlying database.

This is the way Django was designed to behave, but if during development you have constant changes to those attributes, and would like to prevent them from generating migrations, use this snippet. Place it anywhere that gets imported when your project runs (particularly on the command makemigrations).

You may add/remove fields from the list, based on your specific need.

from django.db.models import Field
IGNORED_ATTRS = ['verbose_name', 'help_text', 'choices', 'validators']
original_deconstruct = Field.deconstruct
def new_deconstruct(self):
"""Replacement for the deconstruct() method that simply ignored the attributes above."""
name, path, args, kwargs = original_deconstruct(self)
for attr in IGNORED_ATTRS:
kwargs.pop(attr, None)
return name, path, args, kwargs
Field.deconstruct = new_deconstruct
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment