Django没有分配任何主键

时间:2013-01-07 15:36:05

标签: python django

保存我的表时,Django似乎没有分配任何主键?我错过了什么?

class Campaign(models.Model):
    campaignid = models.CharField(max_length=255, primary_key=True, db_column='campaignID')
    name = models.CharField(max_length=105)
    active = HibernateBooleanField(default=False)
    created = models.DateTimeField()
    modified = models.DateTimeField(null=True, blank=True)
    companyid = models.ForeignKey(Company, null=True, db_column='companyID', blank=True)

    class Meta:
        db_table = u'campaign'


    def __unicode__(self):
        return self.name  

1 个答案:

答案 0 :(得分:0)

好的,因为我的模型是使用guid从现有表生成的,我必须保留它作为CharField添加它似乎工作:

def save(self, *args, **kwargs):
        if not self.campaignid:
            self.campaignid = hashlib.sha1(str(random.random())).hexdigest()
        super(Campaign, self).save(*args, **kwargs)