4
4
from django .db .models import ForeignKey , Q
5
5
from django .db .models .fields import NOT_PROVIDED
6
6
from django .db .models .fields .related import (
7
- RelatedField , lazy_related_operation ,
7
+ RelatedField ,
8
+ lazy_related_operation ,
8
9
)
9
10
from django .utils .deconstruct import deconstructible
10
11
from django .utils .functional import LazyObject , empty
@@ -21,15 +22,15 @@ def __init__(self, field, limit_choices_to):
21
22
22
23
@property
23
24
def value (self ):
24
- subclasses_lookup = self .field .polymorphic_type .subclasses_lookup ('pk' )
25
+ subclasses_lookup = self .field .polymorphic_type .subclasses_lookup ("pk" )
25
26
limit_choices_to = self .limit_choices_to
26
27
if limit_choices_to is None :
27
28
limit_choices_to = subclasses_lookup .copy ()
28
29
elif isinstance (limit_choices_to , dict ):
29
30
limit_choices_to = dict (limit_choices_to , ** subclasses_lookup )
30
31
elif isinstance (limit_choices_to , Q ):
31
32
limit_choices_to = limit_choices_to & Q (** subclasses_lookup )
32
- self .__dict__ [' value' ] = limit_choices_to
33
+ self .__dict__ [" value" ] = limit_choices_to
33
34
return limit_choices_to
34
35
35
36
def __call__ (self ):
@@ -42,8 +43,8 @@ def __init__(self, remote_field, db):
42
43
self .__dict__ .update (remote_field = remote_field , db = db )
43
44
44
45
def _setup (self ):
45
- remote_field = self .__dict__ .get (' remote_field' )
46
- db = self .__dict__ .get ('db' )
46
+ remote_field = self .__dict__ .get (" remote_field" )
47
+ db = self .__dict__ .get ("db" )
47
48
self ._wrapped = remote_field .model ._default_manager .using (db ).complex_filter (
48
49
remote_field .limit_choices_to ()
49
50
)
@@ -53,7 +54,7 @@ def __getattr__(self, attr):
53
54
# Django 2.1+ in order to clear possible cached results.
54
55
# Since no results might have been cached before _setup() is called
55
56
# it's safe to keep deferring until something else is accessed.
56
- if attr == ' all' and self ._wrapped is empty :
57
+ if attr == " all" and self ._wrapped is empty :
57
58
return lambda : self
58
59
return super ().__getattr__ (attr )
59
60
@@ -79,32 +80,35 @@ def __repr__(self):
79
80
80
81
class PolymorphicTypeField (ForeignKey ):
81
82
default_error_messages = {
82
- ' invalid' : _ (' Specified model is not a subclass of %(model)s.' )
83
+ " invalid" : _ (" Specified model is not a subclass of %(model)s." )
83
84
}
84
- description = _ (
85
- 'Content type of a subclass of %(type)s'
86
- )
85
+ description = _ ("Content type of a subclass of %(type)s" )
87
86
default_kwargs = {
88
- 'to' : ' contenttypes.contenttype' ,
89
- ' related_name' : '+' ,
87
+ "to" : " contenttypes.contenttype" ,
88
+ " related_name" : "+" ,
90
89
}
91
90
92
91
def __init__ (self , polymorphic_type , * args , ** kwargs ):
93
92
self .polymorphic_type = polymorphic_type
94
93
self .overriden_default = False
95
94
for kwarg , value in self .default_kwargs .items ():
96
95
kwargs .setdefault (kwarg , value )
97
- kwargs ['limit_choices_to' ] = LimitChoicesToSubclasses (self , kwargs .pop ('limit_choices_to' , None ))
96
+ kwargs ["limit_choices_to" ] = LimitChoicesToSubclasses (
97
+ self , kwargs .pop ("limit_choices_to" , None )
98
+ )
98
99
super ().__init__ (* args , ** kwargs )
99
100
100
101
def contribute_to_class (self , cls , name ):
101
102
super ().contribute_to_class (cls , name )
102
103
polymorphic_type = self .polymorphic_type
103
- if ( isinstance (polymorphic_type , str ) or
104
- polymorphic_type . _meta . pk is None ):
104
+ if isinstance (polymorphic_type , str ) or polymorphic_type . _meta . pk is None :
105
+
105
106
def resolve_polymorphic_type (model , related_model , field ):
106
107
field .do_polymorphic_type (related_model )
107
- lazy_related_operation (resolve_polymorphic_type , cls , polymorphic_type , field = self )
108
+
109
+ lazy_related_operation (
110
+ resolve_polymorphic_type , cls , polymorphic_type , field = self
111
+ )
108
112
else :
109
113
self .do_polymorphic_type (polymorphic_type )
110
114
@@ -115,49 +119,61 @@ def do_polymorphic_type(self, polymorphic_type):
115
119
self .overriden_default = True
116
120
self .polymorphic_type = polymorphic_type
117
121
self .type = polymorphic_type .__name__
118
- self .error_messages ['invalid' ] = (
119
- 'Specified content type is not of a subclass of %s.' % polymorphic_type ._meta .object_name
122
+ self .error_messages ["invalid" ] = (
123
+ "Specified content type is not of a subclass of %s."
124
+ % polymorphic_type ._meta .object_name
120
125
)
121
126
122
127
def check (self , ** kwargs ):
123
128
errors = super ().check (** kwargs )
124
129
if isinstance (self .polymorphic_type , str ):
125
- errors .append (checks .Error (
126
- ("Field defines a relation with model '%s', which "
127
- "is either not installed, or is abstract." ) % self .polymorphic_type ,
128
- id = 'fields.E300' ,
129
- ))
130
+ errors .append (
131
+ checks .Error (
132
+ (
133
+ "Field defines a relation with model '%s', which "
134
+ "is either not installed, or is abstract."
135
+ )
136
+ % self .polymorphic_type ,
137
+ id = "fields.E300" ,
138
+ )
139
+ )
130
140
elif not issubclass (self .polymorphic_type , BasePolymorphicModel ):
131
- errors .append (checks .Error (
132
- "The %s type is not a subclass of BasePolymorphicModel." % self .polymorphic_type .__name__ ,
133
- id = 'polymodels.E004' ,
134
- ))
141
+ errors .append (
142
+ checks .Error (
143
+ "The %s type is not a subclass of BasePolymorphicModel."
144
+ % self .polymorphic_type .__name__ ,
145
+ id = "polymodels.E004" ,
146
+ )
147
+ )
135
148
return errors
136
149
137
150
def formfield (self , ** kwargs ):
138
- db = kwargs .pop (' using' , None )
151
+ db = kwargs .pop (" using" , None )
139
152
if isinstance (self .polymorphic_type , str ):
140
153
raise ValueError (
141
- "Cannot create form field for %r yet, because its related model %r has not been loaded yet" % (
142
- self .name , self .polymorphic_type
143
- )
154
+ "Cannot create form field for %r yet, because its related model %r has not been loaded yet"
155
+ % (self .name , self .polymorphic_type )
144
156
)
145
157
defaults = {
146
- ' form_class' : forms .ModelChoiceField ,
147
- ' queryset' : LazyPolymorphicTypeQueryset (self .remote_field , db ),
148
- ' to_field_name' : self .remote_field .field_name ,
158
+ " form_class" : forms .ModelChoiceField ,
159
+ " queryset" : LazyPolymorphicTypeQueryset (self .remote_field , db ),
160
+ " to_field_name" : self .remote_field .field_name ,
149
161
}
150
162
defaults .update (kwargs )
151
163
return super (RelatedField , self ).formfield (** defaults )
152
164
153
165
def deconstruct (self ):
154
166
name , path , args , kwargs = super ().deconstruct ()
155
- opts = getattr (self .polymorphic_type , '_meta' , None )
156
- kwargs ['polymorphic_type' ] = "%s.%s" % (opts .app_label , opts .object_name ) if opts else self .polymorphic_type
167
+ opts = getattr (self .polymorphic_type , "_meta" , None )
168
+ kwargs ["polymorphic_type" ] = (
169
+ "%s.%s" % (opts .app_label , opts .object_name )
170
+ if opts
171
+ else self .polymorphic_type
172
+ )
157
173
for kwarg , value in list (kwargs .items ()):
158
174
if self .default_kwargs .get (kwarg ) == value :
159
175
kwargs .pop (kwarg )
160
176
if self .overriden_default :
161
- kwargs .pop (' default' )
162
- kwargs .pop (' limit_choices_to' , None )
177
+ kwargs .pop (" default" )
178
+ kwargs .pop (" limit_choices_to" , None )
163
179
return name , path , args , kwargs
0 commit comments