0

保存我的表时,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  
4

1 回答 1

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)
于 2013-01-07T15:58:17.413 回答