Python Django模型:保存模型时为每个标识符生成序列号
你好 我是Django python web开发的新手。 我有一个场景,需要根据Python Django模型:保存模型时为每个标识符生成序列号,python,django,django-models,Python,Django,Django Models,你好 我是Django python web开发的新手。 我有一个场景,需要根据location\u code自动生成一个属性号和一组序列号,作为属性标签的一部分 格式为0000-00-00-0001-000000000,格式定义为“年度”-“总账账户”-“子账户”-“序列号”-“位置代码” 例如: 位置代码:00000000 1-办公室 2021-10-10-0001-00000000 1-表 2021-10-10-0002-00000000 1-椅子 2021-10-10-0003-0000
location\u code
自动生成一个属性号和一组序列号,作为属性标签的一部分
格式为0000-00-00-0001-000000000
,格式定义为“年度”-“总账账户”-“子账户”-“序列号”-“位置代码”
例如:
位置代码:00000000 1-办公室
2021-10-10-0001-00000000 1-表
2021-10-10-0002-00000000 1-椅子
2021-10-10-0003-00000000 1-计算机组
位置代码:00000000 2-仓库办公室
2021-10-10-0001-00000000 2-表
2021-10-10-0002-00000000 2-长表
2021-10-10-0003-00000000 2-咖啡机
诸如此类
除了系列号
之外,我已经可以使用部分属性号保存模型。我知道你可以生成随机数或字符串,但这不是我需要的。以下是到目前为止我所拥有的
class Item(models.Model):
item = models.CharField(max_length=100, null=True)
description = models.TextField(max_length=200, null=True)
date_acquired = models.DateField(max_length=10, null=True)
old_property_number = models.CharField(max_length=100, null=True)
property_number = models.CharField(max_length=100, null=True, blank=True)
unit_of_measure = models.ForeignKey('Uom', related_name='uom_item', null=True, on_delete=models.SET_NULL)
unit_value = models.DecimalField(max_digits=12, decimal_places=2, null=True)
quantity_per_card = models.CharField(max_length=100, null=True, default='1')
quantity_per_physical_count = models.CharField(max_length=100, null=True, default='1' )
location = models.ForeignKey('Location', related_name='location_item', null=True, on_delete=models.SET_NULL)
condition = models.ForeignKey('Condition', related_name='condition_item', null=True, on_delete=models.SET_NULL)
acountable_person = models.ForeignKey('Personnel', related_name='personnel_item', null=True, on_delete=models.SET_NULL)
remarks = models.ForeignKey('Remarks', related_name='remarks_item', null=True, on_delete=models.SET_NULL)
sub_major_group_and_gl_account = models.ForeignKey('Account', related_name='sub_major_group_and_gl_account_item', null=True, on_delete=models.SET_NULL)
class Meta:
verbose_name_plural = 'Item'
def __str__(self):
return '%s - %s - %s' % (self.property_number, self.item, self.description)
# This is where I override the save function to save the format in the 'property_number' field #
def save(self, *args, **kwargs):
self.property_number = '{}-{}-{}-{}'.format(self.date_acquired.year, self.sub_major_group_and_gl_account.sub_major_group_and_gl_account, 'series_number', self.location.location_code)
super(Item, self).save(*args, **kwargs)
管理站点:
我在这里回答了这个问题:
作为参考,以下是我发布的答案:
我在回答我自己的问题
我用下面的save
功能解决了我的问题。如果有人能改进我的解决方案,我们将不胜感激
注意:这仅适用于通过管理站点新添加的实例,如果通过django导入导出导入实例,将不起作用。因此,对于我来说,在使用管理站点中的导入功能时,为每个实例自动分配数据计数器
,这将是剩下的问题
否则,将在excel文件中手动执行分配。XD
你能分享你的模型吗?@Iain Shelvington,我在帖子中加上了它。谢谢
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)