线程安全字段值计数

时间:2011-12-10 16:17:08

标签: django orm django-models thread-safety

有没有办法在django做这样的事情?

INSERT INTO t VALUES(1,(SELECT x.c2 FROM t x ORDER BY c2 DESC LIMIT 1)+1,1);

我有一个包含许多字段的模型。并且必须根据之前的记录设置其中一个字段的值。

目前我通过简单选择以前的记录来做到这一点。但这很可怕而且不是线程安全的。

def save(self, *args, **kw):
    if self.sn is None or self.sn == ns.DEFAULT_GIFT_SN:
        q = Gift.objects.filter(company = self.company).only('id').order_by('-id')
        if q.count():
            last = q[0]
            next_id = int(last.sn) + 1
        else:
            next_id = 1
        self.sn = next_id
    super(Gift, self).save(*args, **kw)

我想......懒得像:

def save(self, *args, **kw):
    if self.sn is None or self.sn == ns.DEFAULT_GIFT_SN:
        self.sn = _F('SELECT x.c2 FROM t x ORDER BY c2 DESC LIMIT 1')
    super(Gift, self).save(*args, **kw)

有什么建议吗?

更新(针对Cesar):

class Gift(models.Model):
    company = models.ForeignKey(Company)
    certificate = ChainedForeignKey(Certificate,
        chained_field = "company",
        chained_model_field = "company",
        show_all = False,
        auto_choose = True
    )
    gifter = models.ForeignKey(User, null = True, blank = True)
    giftant_first_name = models.CharField(_('first name'), max_length = 30, blank = True)
    giftant_last_name = models.CharField(_('last name'), max_length = 30, blank = True)
    giftant_email = models.EmailField(_('e-mail address'), blank = True)
    giftant_mobile = models.CharField(max_length = 20, blank = True, null = True)
    due_date = models.DateTimeField()
    exp_date = models.DateTimeField()
    sn = models.CharField(max_length = 32, default = ns.DEFAULT_GIFT_SN, editable = False)
    pin_salt = models.CharField(max_length = 5, editable = False, default = gen_salt)
    pin = models.CharField(max_length = 32, null = True, blank = True)
    postcard = models.ForeignKey(Postcard)
    state_status = models.IntegerField(choices = ns.STATE_STATUSES, default = ns.READY_STATE_STATUS)
    delivery_status = models.IntegerField(choices = ns.DELIVERY_STATUSES, default = ns.WAITING_DELIVERY_STATUS)
    payment_status = models.IntegerField(choices = ns.PAYMENT_STATUSES, default = ns.NOT_PAID_PAYMENT_STATUS)
    usage_status = models.IntegerField(choices = ns.USAGE_STATUSES, default = ns.NOT_USED_USAGE_STATUS)
    nominal = models.FloatField(default = 0)
    used_money = models.FloatField(default = 0)
        use_date = models.DateTimeField(blank = True, null = True)
    pub_date = models.DateTimeField(auto_now_add = True)
    cashier = ChainedForeignKey(CompanyUserProfile, blank = True, null = True,
        chained_field = "company",
        chained_model_field = "company",
        show_all = False,
        auto_choose = True
    )

    class Meta:
        unique_together = ('company', 'sn',)

2 个答案:

答案 0 :(得分:1)

我知道while是邪恶的,但你可以尝试这样的事情:

sn_is_ok = False
while not sn_is_ok:
    last_id = MyModel.objects.latest('id').id
    try:
        self.sn = last_id + 1
        self.save()
    Except IntegrityError:
        continue
    else:
        sn_is_ok = True

我认为你不会得到更多的2个循环。

答案 1 :(得分:0)

我想你可以做三件事,从简单到极端疯狂:

  1. 如果您可以离开而不需要为每家公司单独增加值,请Gift.sn成为AutoField
  2. 使用unique_together = ('company', 'sn')约束并在唯一性约束失败时更新礼品实例,使用提供的示例@Stan,最坏情况如果您有大量并发写入,则每个只会传入一个转动。
  3. 如果数据库支持,请编写一些自定义SQL以获取Gift模型表上的锁。