在开发项目的时候,我们有时候需要批量的创建对象:为了描述问题方便,创建下面的 model
class SQLExecRecordAgg(models.Model):
parent_rule = models.ForeignKey(TransRule)
trans_row = models.PositiveIntegerField(default=0, help_text=_('the rows affected'))
stats_mins = models.DateTimeField()
create_datetime = models.DateTimeField(auto_now_add=True)
update_datetime = models.DateTimeField(auto_now=True)
def __unicode__(self):
# To avoid querying the foreign table when print this Model object
# So do NOT use the foreign key column object or their property in __unicode__ function
return "id = {0}, parent_rule_id = {1}, trans_row = {2}, " \
"stats_time = {3}".format(self.id, self.parent_rule_id, self.trans_row, self.stats_mins)
class Meta:
db_table = 'sql_exec_record_agg'
unique_together = ['stats_mins', 'parent_rule']
value_list = list()
value_list.append(SQLExecRecordAgg(parent_rule_id=44, trans_row=50,stats_mins=right_side))
value_list.append(SQLExecRecordAgg(parent_rule_id=43, trans_row=50,stats_mins=right_side))
objs = SQLExecRecordAgg.objects.bulk_create(value_list)