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.

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:

1

You can use python manage.py makemigrations app_name --dry-run -v 3 to check the changes in the apps models before running actual makemigrations.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật