简体   繁体   English

当某个时间值从false回到true

[英]when a certain time value comes from false back to true

I need to make it automatically when it comes leave_date (leaveted date) room_bool == True now it's all done via urls.py /get/by/<int:pk>我需要在它出现leave_date room_bool == True /get/by/<int:pk>

views.py

    date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    reg = Registration.objects.get(rooms_id=pk)
    room = Rooms.objects.get(pk=pk)
    a = reg.leave_date.replace(tzinfo=None) >= datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S').replace(
        tzinfo=None)
    if a != True:
        reg.room_bool = True
        reg.save()
        room.room_bool = True
        room.save()

models.py

    room_num = models.IntegerField()
    room_bool = models.BooleanField(default=True) #if room_bool = True room is open, else closed.
    category = models.CharField(max_length=150)

    def __str__(self):
        return f'{self.room_num}'

    class Meta:
        verbose_name = 'Room'

#This class Registration for visitors, to open room for the them
class Registration(models.Model):
    rooms = models.ForeignKey(Rooms, on_delete=models.CASCADE)
    first_name = models.CharField(max_length=150)
    last_name = models.CharField(max_length=150)
    admin = models.ForeignKey(User, on_delete=models.CASCADE)
    pasport_serial_num = models.CharField(max_length=100)
    birth_date = models.DateField()
    img = models.FileField()
    visit_date = models.DateTimeField()
    leave_date = models.DateTimeField()
    guest_count = models.IntegerField()

    def func(self):
        room = Rooms.objects.filter(room_bool=True)
        for i in room:
            if i.room_num == int(self.rooms):
                i.room_bool = False #this should change when adding new registration
                i.save()

You need something to control timing, it's not easy achieved.你需要一些东西来控制时间,这并不容易实现。

Easier way is to create model called Reservation or something u prefer.更简单的方法是创建名为Reservation的 model 或您喜欢的名称。 Then if someone reserved - create that object. Room can check reservations with methods.然后,如果有人预订 - 创建 object。房间可以使用方法检查预订。

class Reservation(...):
    room = models.ForeignKey(...)
    start_date = models.DateTimeField(...)
    end_date = models.DateTimeField(...)
    ...

class Room(...):
    ...

    def is_avaiable(self, date):
        return not self.reservation_set.filter(start_date__gte=date, end_date__lte=date)

Alternatively use something like apscheduler .或者使用类似apscheduler的东西。 Longer way is to use Celery , but use it only if you plan to have a lot of actions like this in many apps.更长的方法是使用Celery ,但仅当您计划在许多应用程序中执行大量此类操作时才使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 python 中的 function 返回值时,它返回为空 - When returning a value from a function in python, it comes back empty 创建带有值的字典时,将值任意从 False 更改为 True - When creating a dictionary with values, the value is arbitrarily changed from False to True 我如何才能将布尔值设置为True,并使其满足条件,则变为False,但仅在一定时间内? - How could I set a boolean value to True, and make it so if a condition is met, it becomes False, but only for a certain amount of time? 在Python中将更新值从true更改为false - Changing update value from true to false in Python 使用矛盾的命令将值从“真”更改为“假” - Changing a value to 'False' from 'True' with contradicting commands 从观察为真时,AssertEqual返回False - AssertEqual returning False when, from Observation is True 数据框真假值 - Dataframe True False Value 从网站检索数据时列表返回为空; 蟒蛇 - List comes back as empty when retrieveing data from website ; Python 如何在经过特定时间后自动将特定字段标记为 False 或 true? - How to automatically mark a specific field either False or true after certain time has been elapsed? 从字典中获取列表中元素的True或False值 - Getting True or False value for elements in a list from a dictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM