赞
踩
model
# 编号自增字段
class Bh(BaseModel):
key = models.CharField(null=True, max_length=128, verbose_name="唯一值", db_index=True, unique=True)
bh = models.IntegerField(verbose_name="编号", db_index=True)
class Meta:
ordering = ['key']
db_table = 'project_bh'
verbose_name = '编号自增'
生成编号的方法
from reclamation.models import Bh
from utils.exceptions import APIException
def get_bh(key):
for i in range(3):
obj, created = Bh.objects.get_or_create(key=key)
raw_bh = obj.bh
record_count = Bh.objects.filter(key=key, bh=raw_bh).update(bh=raw_bh + 1)
if record_count == 1:
return raw_bh + 1
raise APIException(message="配号错误,请重新获取", code=51)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。