@@ -945,3 +945,81 @@ def test_on_delete_do_nothing_maps_to_no_action(self):
945945 assert result is not None
946946 _ , source = result
947947 assert "OnDelete.NO_ACTION" in source
948+
949+
950+ # ---------------------------------------------------------------------------
951+ # MRO fallback tests for render_field_source
952+ # ---------------------------------------------------------------------------
953+
954+
955+ class TestRenderFieldSourceMROFallback :
956+ """MRO fallback resolves custom field types in source rendering."""
957+
958+ def test_render_field_source_custom_field_mro_fallback (self ):
959+ """Custom CharField subclass resolves via MRO in source rendering."""
960+ from django .db import models as django_models
961+
962+ class CustomIDField (django_models .CharField ):
963+ def get_internal_type (self ):
964+ return "CustomIDField"
965+
966+ django_field = CustomIDField (max_length = 36 )
967+ info = _make_field_info (
968+ name = "custom_id" ,
969+ internal_type = "CustomIDField" ,
970+ max_length = 36 ,
971+ primary_key = True ,
972+ django_field = django_field ,
973+ )
974+ result = render_field_source (info )
975+ assert result is not None
976+ assert "fields.CharField(" in result
977+
978+ def test_render_field_source_custom_field_no_django_field (self ):
979+ """Unknown type with django_field=None returns None in source rendering."""
980+ info = _make_field_info (
981+ internal_type = "CustomUnknownField" ,
982+ django_field = None ,
983+ )
984+ result = render_field_source (info )
985+ assert result is None
986+
987+ def test_render_model_source_with_custom_pk_field (self ):
988+ """Model with custom PK field generates valid ModelSourceResult."""
989+ from django .db import models as django_models
990+
991+ class CustomIDField (django_models .CharField ):
992+ def get_internal_type (self ):
993+ return "CustomIDField"
994+
995+ django_field = CustomIDField (max_length = 36 )
996+
997+ fi = _make_field_info (
998+ name = "id" ,
999+ internal_type = "CustomIDField" ,
1000+ column = "id" ,
1001+ max_length = 36 ,
1002+ primary_key = True ,
1003+ django_field = django_field ,
1004+ )
1005+
1006+ class _DummyModel :
1007+ __name__ = "CustomPKModel"
1008+ __module__ = "tests.testapp.models"
1009+
1010+ model_info = ModelInfo (
1011+ model_class = _DummyModel ,
1012+ app_label = "test" ,
1013+ model_name = "custompkmodel" ,
1014+ db_table = "test_custompkmodel" ,
1015+ fields = [fi ],
1016+ unique_together = [],
1017+ is_abstract = False ,
1018+ is_proxy = False ,
1019+ is_managed = True ,
1020+ pk_name = "id" ,
1021+ )
1022+ result = render_model_source (model_info , "django_tortoise" , {})
1023+ assert result is not None
1024+ assert isinstance (result , ModelSourceResult )
1025+ assert "fields.CharField(" in result .source
0 commit comments