11
11
from polymorphic .managers import PolymorphicManager
12
12
from polymorphic .models import PolymorphicTypeInvalid , PolymorphicTypeUndefined
13
13
from polymorphic .tests .models import (
14
+ AliasProxyChild ,
14
15
ArtProject ,
15
16
Base ,
16
17
BlogA ,
87
88
UUIDPlainC ,
88
89
UUIDProject ,
89
90
UUIDResearchProject ,
91
+
92
+ NonAliasNonProxyChild ,
93
+ TradProxyOnProxyChild ,
94
+ TradProxyChild ,
95
+ AliasOfNonProxyChild ,
96
+ ProxyChildAliasProxy ,
97
+
90
98
)
91
99
92
100
@@ -875,6 +883,61 @@ def test_queryset_on_proxy_model_does_not_return_superclasses(self):
875
883
self .assertEqual (5 , ProxyBase .objects .count ())
876
884
self .assertEqual (3 , ProxyChild .objects .count ())
877
885
886
+ def test_queryset_on_polymorphic_proxy_model_returns_superclasses (self ):
887
+ ProxyBase .objects .create (some_data = "Base1" )
888
+ AliasProxyChild .objects .create (some_data = "ProxyChild1" )
889
+ AliasProxyChild .objects .create (some_data = "ProxyChild2" )
890
+ ProxyChild .objects .create (some_data = "PolyChild1" )
891
+ NonAliasNonProxyChild .objects .create (some_data = "SubChild1" )
892
+ NonAliasNonProxyChild .objects .create (some_data = "SubChild2" )
893
+ NonProxyChild .objects .create (some_data = "NonProxChild1" , name = "t1" )
894
+
895
+ with self .subTest (" superclasses" ):
896
+ self .assertEqual (7 , ProxyBase .objects .count ())
897
+ self .assertEqual (7 , AliasProxyChild .objects .count ())
898
+ with self .subTest ("only compete classes" ):
899
+ # Non proxy models should not return the proxy siblings
900
+ self .assertEqual (1 , ProxyChild .objects .count ())
901
+ self .assertEqual (2 , NonAliasNonProxyChild .objects .count ())
902
+ self .assertEqual (3 , NonProxyChild .objects .count ())
903
+
904
+ def test_polymorphic_proxy_object_has_different_ctype_from_base (self ):
905
+ obj1 = ProxyBase .objects .create (some_data = "Base1" )
906
+ obj2 = AliasProxyChild .objects .create (some_data = "ProxyChild1" )
907
+ obj1_ctype = ContentType .objects .get_for_model (
908
+ obj1 , for_concrete_model = False )
909
+ obj2_ctype = ContentType .objects .get_for_model (
910
+ obj2 , for_concrete_model = False )
911
+ self .assertNotEqual (obj1_ctype , obj2_ctype )
912
+
913
+ def test_can_create_django_style_proxy_classes_alias (self ):
914
+ ProxyBase .objects .create (some_data = "Base1" )
915
+ TradProxyChild .objects .create (some_data = "Base2" )
916
+ self .assertEqual (2 , ProxyBase .objects .count ())
917
+ self .assertEqual (2 , TradProxyChild .objects .count ())
918
+ TradProxyOnProxyChild .objects .create ()
919
+
920
+ def test_convert_back_to_django_style_from_polymorphic (self ):
921
+ ProxyBase .objects .create (some_data = "Base1" )
922
+ ProxyChild .objects .create (some_data = "Base1" )
923
+ TradProxyOnProxyChild .objects .create (some_data = "Base3" )
924
+
925
+ self .assertEqual (3 , ProxyBase .objects .count ())
926
+ self .assertEqual (2 , ProxyChild .objects .count ())
927
+ self .assertEqual (3 , TradProxyOnProxyChild .objects .count ())
928
+
929
+ def test_convert_back_to_django_style_from_polymorphic_stops_at_concrete (self ):
930
+ ProxyBase .objects .create (some_data = "Base1" )
931
+ NonProxyChild .objects .create (some_data = "Base1" )
932
+ AliasOfNonProxyChild .objects .create (some_data = "Base1" )
933
+
934
+ self .assertEqual (3 , ProxyBase .objects .count ())
935
+ self .assertEqual (2 , NonProxyChild .objects .count ())
936
+ self .assertEqual (2 , AliasOfNonProxyChild .objects .count ())
937
+
938
+ def test_revert_back_to_polymorphic_proxy (self ):
939
+ self .assertFalse (ProxyChildAliasProxy ._meta .polymorphic__proxy )
940
+
878
941
def test_proxy_get_real_instance_class (self ):
879
942
"""
880
943
The call to ``get_real_instance()`` also checks whether the returned model is of the correct type.
@@ -884,12 +947,12 @@ def test_proxy_get_real_instance_class(self):
884
947
name = "Item1"
885
948
nonproxychild = NonProxyChild .objects .create (name = name )
886
949
887
- pb = ProxyBase .objects .get (id = 1 )
950
+ pb = ProxyBase .objects .get (id = nonproxychild . pk )
888
951
self .assertEqual (pb .get_real_instance_class (), NonProxyChild )
889
952
self .assertEqual (pb .get_real_instance (), nonproxychild )
890
953
self .assertEqual (pb .name , name )
891
954
892
- pbm = NonProxyChild .objects .get (id = 1 )
955
+ pbm = NonProxyChild .objects .get (id = nonproxychild . pk )
893
956
self .assertEqual (pbm .get_real_instance_class (), NonProxyChild )
894
957
self .assertEqual (pbm .get_real_instance (), nonproxychild )
895
958
self .assertEqual (pbm .name , name )
0 commit comments