Django Save函数不保存ID

时间:2018-10-05 14:31:25

标签: django

所以我有一个保存功能,但是它不保存对象的ID。 当我看到消息时,没有保存。我该怎么办?问题在这里==>单击新对象时,将显示以下消息:ID为“ None”的停车不存在。也许它被删除了?

但是它在数据库中。

The parking "None | P5 | 2018-10-18 | 2018-10-18" was added successfully.

def save(self):
        list = []
        d = self.parking_on
        while d <= self.parking_off:
            list.append(
                Parking(id=self.id,
                        user=self.user,
                        email=self.email,
                        parking_on=d,
                        parking_off=d,
                        location=self.location,
                        numar_masina=self.numar_masina
                        )
            )
            d = d + timedelta(days=1)
        Parking.objects.bulk_create(list)

class Parking(models.Model):

    PARKING_PLOT = (
        ('P1', 'Parking #1'),('P2', 'Parking #2'), ('P3', 'Parking #3'),
        ('P4', 'Parking #4'),('P5', 'Parking #5'), ('P6', 'Parking #6'),
        ('P7', 'Parking #7'),('P8', 'Parking #8'), ('P9', 'Parking #9'),
        ('P10', 'Parking #10'),('P11', 'Parking #11'), ('P12', 'Parking #12'),
        ('P13', 'Parking #13'),('P14', 'Parking #14'), ('P15', 'Parking #15')

    )
    user = models.ForeignKey(settings.AUTH_USER_MODEL,blank=True, null=True, default=1, on_delete=True)
    email = models.EmailField(blank=True, null=True)
    parking_on = models.DateField(auto_now=False, auto_now_add=False, blank=True, null=True,help_text='Please select the date you want to come in the office.',)
    parking_off = models.DateField(auto_now=False, auto_now_add=False, blank=True, null=True,help_text='Please select the date when you leave')
    numar_masina = models.CharField(max_length=8, default="IF77WXV", blank=True, null=True,help_text='Please insert your license plate number')
    location = models.CharField(max_length=3, blank=True, default="P1", null=True, choices=PARKING_PLOT,help_text='Please select the desired parking plot.')
    updated = models.DateTimeField(auto_now=True, auto_now_add=False, blank=True, null=True)
    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True, blank=True, null=True)

    objects = ParkingManager()

    def __str__(self):
        return str(self.pk) + " | " + self.location + " | " + str(self.parking_on) + " | " + str(self.parking_off)

    class Meta:
        verbose_name_plural = "parking"
        ordering = ["-parking_on"]
        unique_together = ("parking_on", "location")

0 个答案:

没有答案