python - 使用 INT django Python 连接字符串
问题描述
我有我的views.py
class ShopCreateView(CreateView):
fields = ('shop_id',)
model = models.Shop
def form_valid(self, form):
self.object = form.save(commit=False)
dept_obj = models.departemen.objects.get(seller_id=self.object.id)
dept_name = dept_obj.name
date = datetime.datetime.now().date()
year = date.year
month = date.month
seller_count = seller_obj.count()
if seller_count > 999:
counted = seller_count - 999
self.object.shop_id = dept_name + join_date + year + month + counted
self.object.save()
return super(ModelFormMixin, self).form_valid(form)
这是我的解释
dept_name
返回字符串
例如。
PT。柜台Avenger Manequin = CAM _
P T.国际工作= PIJ
我希望我的代码采用字母的第一个字符。但现在不知道该怎么做。找到解决方案,但它仅用于展示template.html
所以self.object.shop_id = dept_name + year + month + counted
会喜欢:
CAM71804001
它的混合字符串和整数。如果 return也最后3
har 1
。我需要成功001
谢谢你!
解决方案
改为使用str.format
。要选择字符串的第一个字母,只需用[0]
. 您可以{:03}
使用三个零传递到零填充,就像这样
self.object.shop_id = "{}{}{}{}{:03}".format(dept_name[0], join_date, year , month, counted)
推荐阅读
- javascript - 为什么 chrome 控制台 jquery 甚至在我分配值之前就为选择选项添加了值
- css - 如何在标签中添加内联样式?
- ios - 如何获取 INTask 的 SiriKit 选择事件?
- javascript - React 在启动时从 api 获取所有数据
- activiti - 不使用 activiti 表时如何将任务分配给候选组?
- server - 如何修复 NoMachine 中的 CAPS LOCK 同步?
- python - 如何从用户那里捕获 3 个点并形成一个三角形?
- amazon-dynamodb - 如何使用 AWS Glue ETL 加载 dynamodb 表
- java - 如何使用 Shell 脚本从 XML 有效负载中提取值
- d3.js - 在 D3 中格式化刻度值