@@ -42,6 +42,13 @@ def add_arguments(self, parser):
42
42
type = int ,
43
43
help = "Set a custom batch size when bulk inserting historical records." ,
44
44
)
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
+ )
45
52
46
53
def handle (self , * args , ** options ):
47
54
self .verbosity = options ["verbosity" ]
@@ -60,7 +67,7 @@ def handle(self, *args, **options):
60
67
if self .verbosity >= 1 :
61
68
self .stdout .write (self .COMMAND_HINT )
62
69
63
- self ._process (to_process , batch_size = options ["batchsize" ])
70
+ self ._process (to_process , batch_size = options ["batchsize" ], default_date = options [ "default_date" ] )
64
71
65
72
def _auto_models (self ):
66
73
to_process = set ()
@@ -109,11 +116,12 @@ def _model_from_natural_key(self, natural_key):
109
116
raise ValueError (msg )
110
117
return model , history_model
111
118
112
- def _bulk_history_create (self , model , batch_size ):
119
+ def _bulk_history_create (self , model , batch_size , default_date = None ):
113
120
"""Save a copy of all instances to the historical model.
114
121
115
122
:param model: Model you want to bulk create
116
123
:param batch_size: number of models to create at once.
124
+ :param default_date: date to set for the historical records.
117
125
:return:
118
126
"""
119
127
@@ -135,7 +143,7 @@ def _bulk_history_create(self, model, batch_size):
135
143
# creating them. So we only keep batch_size worth of models in
136
144
# historical_instances and clear them after we hit batch_size
137
145
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 )
139
147
140
148
instances = []
141
149
@@ -151,9 +159,9 @@ def _bulk_history_create(self, model, batch_size):
151
159
152
160
# create any we didn't get in the last loop
153
161
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 )
155
163
156
- def _process (self , to_process , batch_size ):
164
+ def _process (self , to_process , batch_size , default_date = None ):
157
165
for model , history_model in to_process :
158
166
if history_model .objects .exists ():
159
167
self .stderr .write (
@@ -164,6 +172,6 @@ def _process(self, to_process, batch_size):
164
172
continue
165
173
if self .verbosity >= 1 :
166
174
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 )
168
176
if self .verbosity >= 1 :
169
177
self .stdout .write (self .DONE_SAVING_FOR_MODEL .format (model = model ))
0 commit comments