Django自动增量字段
我有2列名为串行和包的列,我需要自动增加它们,但彼此相互汇总,也基于将更新记录的用户,因此每个袋子都应具有100个序列号并在达到100后自动重置数字,然后开始再次使用2号袋,然后将100个序列放在其中并重置。
例如:
当用户更新第一个记录时,袋子将从数字1开始,串行也将是数字1,第二个记录袋仍将数字1,串行将更改为2号,直到在一个袋中达到100串行,然后我们将从第2号行李和序列号1等开始...
谢谢
I have 2 columns named Serial and Bag I need them to be auto incremented but based on each other and also based on the user that will update the record, so every bag should have 100 serial and reset the number automatically after reaching 100, then start again with Bag number 2 and put 100 serial in it and reset.
For example:
when user update the first record the Bag will start with number 1 and Serial will be also number 1 the second record Bag will still number 1 and the serial will be changed to number 2 till reach 100 Serial in one Bag, then we will start again with bag number 2 and serial number 1 etc ...
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您解释示例的方式有些混乱,但我会尝试给您一个答案。
我假设“ 2列”名为serial and bag 是同一模型的字段,正如您在评论中回答的“ ” 记录已经存在,但它具有空的串行和袋子“,这意味着自动报道在更新记录时开始。最后,您提到了第一个和第二个记录,暗示该模型中有多个记录。基于这些标准,您可以做的就是在模型中添加一个保存方法:
现在,每次写入保存时,例如:
模型保存方法执行。
The way you explain your example is a bit confusing but I'll try to give you an answer.
I assume the "2 columns named Serial and Bag" are fields of the same model and as you replied in the comments "the record is already existing but it has empty serial and bag", which means the auto-increment begins when the record is updated. Lastly, you mentioned first and second records implying that there are multiple records in this model. Based on these criteria, what you can do is add a save method in your model:
Now, each time you write save like:
The model save method executes.
与其进行python中的逻辑,如果可以同时进行多个更新,则应将其推到数据库中。
类似:
可能可以将其转换为django orm,但是仅掉入RAW SQL或通过
.extra()在QuerySet上试图将其插入。
Rather than do the incrementing logic in python, where it is subject to race conditions if multiple updates can happen concurrently, it should be possible to push it down into the database.
Something like:
It might be possible to translate that into django ORM, but it might also be easier and more readable to just drop into raw SQL or sneak it in via
.extra()
on a queryset than attempt to shoehorn it in.