我想创建一个列名" cust_id" .which是自动生成的,如何为管理面板的代码编写代码。 的 models.py
class Account(models.Model):
cust_id=models.CharField(max_length=50,primary_key=True)
我为视图编写了autogenerete cust id的代码,但该代码不适用于管理面板 自动生成cust id的代码
def create(self, request):
queryset=Account.objects.all()
data=request.POST
temp=queryset.aggregate(Max('cust_id'))
print temp
temp=temp.get('cust_id__max')
print "temp>>",temp
cust_id=""
if not temp :
print"before"
cust_id="CUST0001"
print "cust_id=",cust_id
else:
print"after"
print "m=",temp
prefix=temp[0:4]
print "prefix",prefix
suffix=temp[4:]
print "suffix",suffix
int_suffix=int(suffix)
print "prefix=",prefix,"suffix=",suffix,"int_suffix",int_suffix
if int_suffix<9 :
suffix="000"+str(int_suffix+1)
elif int_suffix>=9 and int_suffix<99:
suffix="00"+str(int_suffix+1)
elif int_suffix>=99 and int_suffix<999:
suffix="0"+str(int_suffix+1)
elif int_suffix>=999 and int_suffix<9999:
suffix=""+str(int_suffix+1)
cust_id=prefix+suffix
print "cust_id=",cust_id
account=Account()
account.cust_id=cust_id
account.cust_first_name=data.get('cust_first_name')
account.cust=data.get('cust_address')
account.is_active=data.get('is_active',default=False)
if account.save():
return Response(status=status.HTTP_201_CREATED)
else:
return Response( status=status.HTTP_400_BAD_REQUEST)
请检查此代码。并帮助我。 提前致谢