@@ -42,6 +42,13 @@ def add_arguments(self, parser):
4242 type = int ,
4343 help = "Set a custom batch size when bulk inserting historical records." ,
4444 )
45+ parser .add_argument (
46+ "--default-date" ,
47+ action = "store" ,
48+ dest = "default_date" ,
49+ default = None ,
50+ help = "Set a custom date for the historical records." ,
51+ )
4552
4653 def handle (self , * args , ** options ):
4754 self .verbosity = options ["verbosity" ]
@@ -60,7 +67,7 @@ def handle(self, *args, **options):
6067 if self .verbosity >= 1 :
6168 self .stdout .write (self .COMMAND_HINT )
6269
63- self ._process (to_process , batch_size = options ["batchsize" ])
70+ self ._process (to_process , batch_size = options ["batchsize" ], default_date = options [ "default_date" ] )
6471
6572 def _auto_models (self ):
6673 to_process = set ()
@@ -109,11 +116,12 @@ def _model_from_natural_key(self, natural_key):
109116 raise ValueError (msg )
110117 return model , history_model
111118
112- def _bulk_history_create (self , model , batch_size ):
119+ def _bulk_history_create (self , model , batch_size , default_date = None ):
113120 """Save a copy of all instances to the historical model.
114121
115122 :param model: Model you want to bulk create
116123 :param batch_size: number of models to create at once.
124+ :param default_date: date to set for the historical records.
117125 :return:
118126 """
119127
@@ -135,7 +143,7 @@ def _bulk_history_create(self, model, batch_size):
135143 # creating them. So we only keep batch_size worth of models in
136144 # historical_instances and clear them after we hit batch_size
137145 if index % batch_size == 0 :
138- history .bulk_history_create (instances , batch_size = batch_size )
146+ history .bulk_history_create (instances , batch_size = batch_size , default_date = default_date )
139147
140148 instances = []
141149
@@ -151,9 +159,9 @@ def _bulk_history_create(self, model, batch_size):
151159
152160 # create any we didn't get in the last loop
153161 if instances :
154- history .bulk_history_create (instances , batch_size = batch_size )
162+ history .bulk_history_create (instances , batch_size = batch_size , default_date = default_date )
155163
156- def _process (self , to_process , batch_size ):
164+ def _process (self , to_process , batch_size , default_date = None ):
157165 for model , history_model in to_process :
158166 if history_model .objects .exists ():
159167 self .stderr .write (
@@ -164,6 +172,6 @@ def _process(self, to_process, batch_size):
164172 continue
165173 if self .verbosity >= 1 :
166174 self .stdout .write (self .START_SAVING_FOR_MODEL .format (model = model ))
167- self ._bulk_history_create (model , batch_size )
175+ self ._bulk_history_create (model , batch_size , default_date )
168176 if self .verbosity >= 1 :
169177 self .stdout .write (self .DONE_SAVING_FOR_MODEL .format (model = model ))
0 commit comments