1111import warnings
1212from typing import Dict , TypeVar
1313
14- from django .contrib .auth .hashers import make_password
15- from django .core import files as django_files
16- from django .db import IntegrityError
17-
1814from . import base , declarations , errors
1915
2016logger = logging .getLogger ('factory.generate' )
@@ -124,6 +120,7 @@ def _generate(cls, strategy, params):
124120 @classmethod
125121 def _get_or_create (cls , model_class , * args , ** kwargs ):
126122 """Create an instance of the model through objects.get_or_create."""
123+ from django .db import IntegrityError
127124 manager = cls ._get_manager (model_class )
128125
129126 assert 'defaults' not in cls ._meta .django_get_or_create , (
@@ -193,7 +190,10 @@ def _after_postgeneration(cls, instance, create, results=None):
193190
194191
195192class Password (declarations .Transformer ):
196- def __init__ (self , password , transform = make_password , ** kwargs ):
193+ def __init__ (self , password , transform = None , ** kwargs ):
194+ if transform is None :
195+ from django .contrib .auth .hashers import make_password
196+ transform = make_password
197197 super ().__init__ (password , transform = transform , ** kwargs )
198198
199199
@@ -207,6 +207,7 @@ def _make_data(self, params):
207207 return params .get ('data' , b'' )
208208
209209 def _make_content (self , params ):
210+ from django .core .files .base import ContentFile , File
210211 path = ''
211212
212213 from_path = params .get ('from_path' )
@@ -222,21 +223,21 @@ def _make_content(self, params):
222223 if from_path :
223224 path = from_path
224225 with open (path , 'rb' ) as f :
225- content = django_files . base . ContentFile (f .read ())
226+ content = ContentFile (f .read ())
226227
227228 elif from_file :
228229 f = from_file
229- content = django_files . File (f )
230+ content = File (f )
230231 path = content .name
231232
232233 elif from_func :
233234 func = from_func
234- content = django_files . File (func ())
235+ content = File (func ())
235236 path = content .name
236237
237238 else :
238239 data = self ._make_data (params )
239- content = django_files . base . ContentFile (data )
240+ content = ContentFile (data )
240241
241242 if path :
242243 default_filename = os .path .basename (path )
@@ -248,8 +249,9 @@ def _make_content(self, params):
248249
249250 def evaluate (self , instance , step , extra ):
250251 """Fill in the field."""
252+ from django .core .files .base import File
251253 filename , content = self ._make_content (extra )
252- return django_files . File (content .file , filename )
254+ return File (content .file , filename )
253255
254256
255257class ImageField (FileField ):
0 commit comments