有没有一种方法可以将新Django对象的id值设置为从某个值开始? [英] Is there a way to set the id value of new Django objects to start at a certain value?

查看:363
本文介绍了有没有一种方法可以将新Django对象的id值设置为从某个值开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以为Django中的对象设置id值的计数器?

Is there a way to set the counter of id values for objects in Django?

例如,我有一组对象 Video,它们当前位于id = 100下次创建视频类型的对象时,它将为id = 101。但是,我希望所有新创建的Video对象都从id = 2000开始。有办法吗?

For example I have an set of objects 'Video' that are currently at id=100 the next time I create an object of type 'Video' it will be id=101. However I'd like to have all newly created Video objects start at id=2000. is there a way to do this?

推荐答案

您可以制作一个夹具来在数据库中创建一个ID = 1999的虚拟行。 。然后,下一个自动生成的ID将为2000。

You can make a fixture that creates a dummy row in your database with ID=1999. The next auto-generated ID would be 2000 then.

此解决方案可能取决于您的数据库后端是否恢复您离开的地方。至少MySql和PostgreSQL可以做到这一点(我猜每个后端都可以做到这一点)。

This solution probably depends on you database backend to 'resume where you left off'. At least MySql and PostgreSQL do this (I guess every backend does this).

(据我所知)还有一种仅针对MySQL的解决方案,该解决方案由SQL组成语句 ALTER TABLE tbl AUTO_INCREMENT = 100;

There's also a solution for MySql only (as far as I know) that consists of a SQL statement ALTER TABLE tbl AUTO_INCREMENT = 100;

不过,这将不再与数据库无关。

That wouldn't be database agnostic anymore, though.

这篇关于有没有一种方法可以将新Django对象的id值设置为从某个值开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆