如何在保存时使用ID填充字段,但ID不存在

时间:2019-05-28 13:44:32

标签: django

我正在构建一个服务台系统,我希望根据ID的长度自动指定票证。 例: 票证的ID为47,所以我希望使用5个整数作为长号->#00047,或者ID 2021的ID应该为#02021

我的相关代码: 我的模特:

class Ticket(models.Model):
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    designation = models.CharField(max_length=255, blank=True, null=True)

我尝试在自己的views.py

中用for循环填充字段。
 def Index(request):
    ticket = Ticket.objects.all()
    for t in ticket:
      id = len(str(t.id))
      if id == 1:
        t.designation = "#" + str("0000") + str(t.id)
      elif id == 2:
        t.designation = "#" + str("000") + str(t.id)
      elif id == 3:
        t.designation = "#" + str("00") + str(t.id)
      elif id == 4:
        t.designation = "#" + str("0") + str(t.id)
      else:
        t.designation = "#" + str(t.id)
    context = {'ticket':ticket}
    return render(request, 'ticketsystem/index.html', context)

并且输出是正确的,但是我想在创建对象时得到填充量

我通过将其添加到我的票证模型中来尝试以下操作:

class Ticket(models.Model):
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    designation = models.CharField(max_length=255, blank=True, null=True)

    def save(self):
        id = len(str(id))
        if id == 1:
            designation = "#" + str("0000") + str(id)
        elif id == 2:
            designation = "#" + str("000") + str(id)
        elif id == 3:
            designation = "#" + str("00") + str(id)
        elif id == 4:
            designation = "#" + str("0") + str(id)
        else:
            designation = "#" + str(id)
        super().save()

但出现以下错误:

分配前引用的本地变量'id'

2 个答案:

答案 0 :(得分:1)

您需要先运行super调用,否则,您的对象将不在数据库中。

class Ticket(models.Model):
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    designation = models.CharField(max_length=255, blank=True, null=True)

    def save(self):
        if not self.pk:
            super().save()

        str_id = str(self.pk)
        if str_id == 1:
            self.designation = "#0000{}".format(str_id)
        elif str_id == 2:
            self.designation = "#000{}".format(str_id)
        elif str_id == 3:
            self.designation = "#00{}".format(str_id)
        elif str_id == 4:
            self.designation = "#0{}".format(str_id)
        else:
            self.designation = "#{}".format(str_id)

        return self.save()

答案 1 :(得分:0)

您尚未覆盖id字段,因此它将是一个整数,并且您希望让Django处理自动增量等。

(我认为)最好添加一个新字段,该字段接受id并将其转换为字符串

class Ticket(models.Model):
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)

    @property
    def ticketkennung(self):
        this_id = str(self.id)
        if len(this_id) = 1 : str_id = '0000' + this_id
        etc ...
        return this_id

OR

class Ticket(models.Model):
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    designation = models.CharField(max_length=255, blank=True, null=True)

    def save(self):
        this_id = len(str(self.id))
        if id == 1: self.designation = "#" + str("0000") + str(id)
        etc
        super().save(*args, **kwargs)