简体   繁体   中英

Can I set pk(primary key) as a dynamic number in django. If not then how do I set a variable to be dynamic?

I have a Employee Database in which we can add and delete employee. I created some entries and they took the values of id(pk)= 1 ,2 ,3 ,4 etc.

NOW when I deleted one of the Entry (lets say id= 2).

Is there anyway that id(pk) can be updated so as I have one series... and not like pk = 1,3,4,5 etc. (It creates problem while using APIs to extract data)

models.py

from django.db import models
from django.forms.fields import DateField
class Emp(models.Model):
    name = models.CharField(max_length=200)
    depart = models.CharField(max_length= 16, choices = (('FINANCE','FINANCE'),('MARKETING','MARKETING')))
    timestamp = models.DateTimeField(auto_now_add= True, auto_now = False)
    update = models.DateTimeField(auto_now_add = False, auto_now = True)
    def __unicode__(self):
        return self.name

No. And there's no reason why you should do it, or why it should "create a problem" with your API. A PK uniquely identifies a record, and that identifier must stay the same: if anything, changing the PK for a record would be the thing that creates a problem. Don't try.

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