Django更新/查询

时间:2014-09-22 06:08:45

标签: django django-queryset

我试图更新模型中的变量,但它没有发生。不知道为什么?我之前也一直这样做过去曾经工作过,但不知道为什么不发生?任何建议。

我的模特

class VehicleDetails(models.Model):
    vehicle_name = models.CharField(max_length = 255)
    total_shipments = models.CharField(max_length = 255)
    remaining_shipments = models.CharField(max_length = 255)
    status = models.CharField(max_length = 255)
    time = models.DateTimeField( auto_now_add = True, db_index = True)

我想做的事

        vehicle_details_obj = VehicleDetails.objects.filter(status = 'open').order_by('-time')
        try: 
            vehicle_name = vehicle_details_obj[0].vehicle_name
            remaining_shipments = int(vehicle_details_obj[0].remaining_shipments) - 1
            print remaining_shipments
            vehicle_details_obj[0].remaining_shipments = str(remaining_shipments)
            if remaining_shipments == 0: vehicle_details_obj[0].status = 'close'
            vehicle_details_obj[0].save()
        except : 
            vehicle_name = 'NA'

我通过从中减去1来更新remaining_shipments字段,然后更新它。我之前曾经这样更新,但它没有发生。我也试过了vehicle_details_obj[0].update(remaining_shipments = remaining_shipments),但它不起作用。任何帮助都感激不尽。感谢

0 个答案:

没有答案