简体   繁体   中英

Django save integer not null primary key value

I am using django to develop my application. A landmark model class has integer not null primary key field (gid). I want to insert max(gid) + 1 value to that field but can't set it as auto increment. What is the best solution? I thot override save() in model so that i don't want to explicitly assign value to the gid field.

Plz help me to do it in efficiently...

Thanking you ,

Try latest

MyMobel.objects.latest('gid').gid + 1

This will take the latest value. Be careful to check if the model has no objects. In that case, set the gid to the first value you want

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM