Django:经过一段时间后更新模型属性

时间:2019-02-24 08:28:24

标签: django django-forms django-views

我有汽车预订表。保存后,该表单将重定向到视图。视图获取表单ID ,即我的Reservation模型的实例,然后从表单的 chosen_car字段中选择特定的车辆,然后,应更新该车辆的 available 归因于 False

现在,用户预订汽车的时间为固定的时间段。时间过去后,我希望将available模型中特定车辆实例的Vehicle属性设置为True

我试图在视图中使用while循环实现此功能,但它无法正常工作。我希望此过程可以自动化,例如,对于车辆实例,available属性在保留该车辆时更新为False,并更新为True时间过去了。这样,其他用户就可以预订。

# My Vehicle model:

class Vehicle(models.Model):
    car_no = models.CharField (max_length = 20, blank=False)
    car_model = models.ForeignKey (Car_model, on_delete=models.CASCADE)
    garage = models.ForeignKey (Garage, on_delete=models.CASCADE)
    category = models.ForeignKey (Category, on_delete=models.CASCADE)
    available = models.BooleanField(default=True)

# The Reservation model:

class Reservation(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    date_from = models.DateField(auto_now=False, auto_now_add=False, blank=False)
    date_to = models.DateField(auto_now=False, auto_now_add=False, blank=False)
    time_from = models.TimeField(auto_now=False, auto_now_add=False, blank=False)
    time_to = models.TimeField(auto_now=False, auto_now_add=False, blank=False)
    garage = models.ForeignKey(Garage,on_delete=models.CASCADE, blank=False, verbose_name="Start from")
    destination=models.CharField(max_length=20, blank=False, verbose_name="Where will you go")
    chosen_vehicle=models.CharField(max_length = 20, blank=False, default='')
    booking_time=models.DateTimeField(default = datetime.now)

我用来将每辆已预订汽车的Vehile模型的available属性设置为True或False的视图:

def carDBconfigView(request, pk):
    this_form = Reservation.objects.get(pk = pk)
    the_car = this_form.chosen_vehicle
    v = Vehicle.objects.get(car_no = the_car)
    v.available = False
    v.save()
    to = this_form.date_to
    to_time = this_form.time_to

    #while datetime.date.today() > to and datetime.datetime.now().time() > to_time:  # tried this first
    while datetime.datetime.now().time() > to_time:
    while datetime.date.today() > to:
        c = Vehicle.objects.get(car_no = the_car)
        c.available = True
        c.save()
    return redirect('review')

我定义的预订表格:

class NewReservationForm(forms.ModelForm):
    class Meta:
        model= Reservation
        exclude = ['user','chosen_vehicle','garage','booking_time']

如何根据设置的日期将available属性设置为TrueFalse,而不必始终手动将每辆车设置为TrueFalse

1 个答案:

答案 0 :(得分:0)

from的{​​{1}}和to更改为DateTmeField,并将chosen_vehicle更改为ForeignKey

Vehicle

使用此工具时您拥有什么:您可以通过属性来检查车辆是否可用:

class Reservation(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    from = models.DateTimeField(auto_now=False, auto_now_add=False, blank=False)
    to = models.DateTimeField(auto_now=False, auto_now_add=False, blank=False)
    garage = models.ForeignKey(Garage,on_delete=models.CASCADE, blank=False, verbose_name="Start from")
    destination=models.CharField(max_length=20, blank=False, verbose_name="Where will you go")
    chosen_vehicle=models.ForeignKey(Vehicle, on_delete=models.CASCADE, related_name='reservations')
    booking_time=models.DateTimeField(default = datetime.now)


class Vehicle(models.Model):
    car_no = models.CharField (max_length = 20, blank=False)
    car_model = models.ForeignKey (Car_model, on_delete=models.CASCADE)
    garage = models.ForeignKey (Garage, on_delete=models.CASCADE)
    category = models.ForeignKey (Category, on_delete=models.CASCADE)

    @property
    def available(self):
        now = datetime.now()
        # here we check if we have reservation that started before now and
        # will be finished after now
        reservations = self.reservations.filter(from__gte=now, to__lte=now)
        # exists() return boolean True or False
        return reservations.exists()

因此,每次您选择my_vehicle = Vehicle.objects.get(...) my_vehicle.available # will return True or False 时,都会根据当前时间进行计算并返回最新信息。