Created
August 19, 2016 09:03
-
-
Save vdboor/e3754e19551f2fbbcc31b01eec99ee8e to your computer and use it in GitHub Desktop.
Avoid the `verbose_name` and `help_text` in Django migrations for fields.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Patch the creation of database migrations in Django | |
Import this early from `__init__.py``. | |
- Don't want verbose_name changes in the migrations file. | |
- Don't want help_text in the migrations file. | |
""" | |
from functools import wraps | |
from django.db.models import Field | |
def patch_deconstruct(old_func, condition): | |
""" | |
Patch the ``Field.deconstruct`` to remove useless information. | |
This only happens on internal apps, not third party apps. | |
""" | |
@wraps(old_func) | |
def new_deconstruct(self): | |
name, path, args, kwargs = old_func(self) | |
# AutoField has no model on creation, but can be skipped | |
if hasattr(self, 'model') and condition(self): | |
kwargs.pop('verbose_name', None) | |
kwargs.pop('help_text', None) | |
return name, path, args, kwargs | |
return new_deconstruct | |
Field.deconstruct = patch_deconstruct(Field.deconstruct, lambda self: self.model.__module__.startswith('apps.')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have not used the command with Django 4.1 yet. You'll have to look at the changelog to see if there's anything to suggest that the API changed.