Python Django保存功能模型:如何分配;数据“U计数器”;从excel文件导入时作为数字系列
这是这些问题的继续: 我有一个为每个位置id生成序列号的场景,我可以在上面的链接中解决这个问题 现在,我关心的是: 使用Python Django保存功能模型:如何分配;数据“U计数器”;从excel文件导入时作为数字系列,python,django,Python,Django,这是这些问题的继续: 我有一个为每个位置id生成序列号的场景,我可以在上面的链接中解决这个问题 现在,我关心的是: 使用django导入导出导入excel文件时,如何将data\u计数器指定为序列号 到目前为止,如果我将通过管理站点添加一个新实例,保存功能正在按预期工作,但是当通过导入功能添加一个新实例(批量)时,一切都乱七八糟,我无法编辑整个属性号字段xD,结果如下: 如图所示,序列号是最后一组(4位)的第二个,位置id是最后一组(2位) 前9个数据应如下所示: 1972-30-40-0
django导入导出
导入excel文件时,如何将data\u计数器
指定为序列号
到目前为止,如果我将通过管理站点添加一个新实例,保存功能
正在按预期工作,但是当通过导入功能添加一个新实例(批量)时,一切都乱七八糟,我无法编辑整个属性号字段xD,结果如下:
如图所示,序列号是最后一组(4位)的第二个,位置id是最后一组(2位)
前9个数据应如下所示:
1972-30-40-0001-00
1972-30-40-0002-00
1972-30-40-0003-00
1972-30-40-0004-00
1972-30-40-0001-18
1972-30-40-0005-00
1972-30-40-0001-19
1972-30-40-0002-19
1972-30-40-0006-00 and so on...
保存功能
def save(self, *args, **kwargs):
data_counter = Item.objects.filter(location=self.location).aggregate(counter=Count('id'))['counter'] # Count existing instance with matching "location id" of the new instance
if self.pk is None and self.property_number is None: # if new instance and property number field is blank
if data_counter is None: # if data_counter is empty/none, no matching instance
data_counter = 1 # start at 1
else: # else, if there is matching instance
data_counter += 1 # add 1 to the data_counter
elif self.pk is not None and self.property_number is None: # if not new instance and property number field is blank, Update an instance without property number
if data_counter is None: # if data_counter is empty/none, the existing instance has no matching location id, Update
data_counter = 1 # Existing instance start at 1
else: # else, if there is matching instance
data_counter += 1 # add 1 to the data_counter
# data_counter will be the series number for every location ID
self.property_number = '{}-{}-{:04d}-{}'.format(self.date_acquired.year, self.sub_major_group_and_gl_account.sub_major_group_and_gl_account, data_counter, self.location.location_code)
super(Item, self).save(*args, **kwargs)