Search code examples
djangodjango-modelsdjango-migrations

How to get more info on what field differences where found by manage makemigrations


Occasionally when running manage makemigrations in my Django project, I end up with an unexpected migrations.AlterField() entry in my migrations file. I am aware that these result from Django seeing differences between that field's definition in my model and the field's definition after applying all migrations.

Is there any way to find out which difference it sees? I can look at my fields in the models and use _meta to get field settings I didn't explicitly specify, but is there any way to get information on what the field looks like on the migrations side of things? Compare the target vs actual state, so to speak?

The info I am looking for is, if there is for instance an AlterField() on an IntegerField, that's been picked up because the max value changed, or it has a different verbose_name.

For now I just manually dig through the migration files for mentions of that respective field, but I am working with a legacy project that has a LOT of them. So I am wondering if anyone ever ran into the same problem and found a better solution.


Solution

  • Django has no means to print this, or at least not without patching the Django package. But the good news is, we can: you can look to the virtual environment for the location where Django is installed, and patch the django/db/migrations/autodetector.py file. Django uses this to detect changes. Indeed, in the source code, we see [GitHub]:

    old_field_dec = self.deep_deconstruct(old_field)
    new_field_dec = self.deep_deconstruct(new_field)
    # ...
    if old_field_dec != new_field_dec and old_field_name == field_name:
        # ...
    

    We can replace this with something that logs the changes, for example with some function to log the difference:

    MISSING = object()
    def show_dict_diff(old, new):
        for k, v1 in new.items():
            v2 = old.get(k, MISSING)
            if v2 is MISSING:
                yield f"  Added: {k} = {v1}"
            elif v1 != v2:
                yield f"  Changed: {k} = {v2} -> {v1}"
        for k, v in old.items():
            if k not in new:
                yield f"  Removed: {k} = {v}"
    

    and adapt the file to:

    old_field_dec = self.deep_deconstruct(old_field)
    new_field_dec = self.deep_deconstruct(new_field)
    # …
    if old_field_dec != new_field_dec and old_field_name == field_name:
        print(f'change detected for {field_name}')
        show_dict_diff(old_field_dec[2], new_field_dec[2])
        # …

    or as a patch:

    Index: django/db/migrations/autodetector.py
    <+>UTF-8
    ===================================================================
    diff --git a/django/db/migrations/autodetector.py b/django/db/migrations/autodetector.py
    --- a/django/db/migrations/autodetector.py  (revision 9dfcb719558c21dbc92754739cc8f4b6e6fdfa62)
    +++ b/django/db/migrations/autodetector.py  (date 1726125682049)
    @@ -19,6 +19,20 @@
     )
     from django.utils.functional import cached_property
     
    +MISSING = object()
    +
    +
    +def show_dict_diff(old, new):
    +    for k, v1 in new.items():
    +        v2 = old.get(k, MISSING)
    +        if v2 is MISSING:
    +            yield f"  Added: {k} = {v1}"
    +        elif v1 != v2:
    +            yield f"  Changed: {k} = {v2} -> {v1}"
    +    for k, v in old.items():
    +        if k not in new:
    +            yield f"  Removed: {k} = {v}"
    +
     
     class OperationDependency(
         namedtuple("OperationDependency", "app_label model_name field_name type")
    @@ -1284,6 +1298,8 @@
                 # db_column was allowed to change which generate_renamed_fields()
                 # already accounts for by adding an AlterField operation.
                 if old_field_dec != new_field_dec and old_field_name == field_name:
    +                print(f'change detected for {field_name}')
    +                show_dict_diff(old_field_dec[2], new_field_dec[2])
                     both_m2m = old_field.many_to_many and new_field.many_to_many
                     neither_m2m = not old_field.many_to_many and not new_field.many_to_many
                     if both_m2m or neither_m2m: