0

I need to discover interval for begin_date and end_date fields of my model

class Reserved(models.Model):
    begin_date = models.DateTimeField()
    end_date = models.DateTimeField()

In short when user is saving some data to the database in forms should be validation. Validation should to check is there reservation in interval begin_date and end_date and give error.

I tried to to this but it does not work :(

def save(self, commit=True):
    date_validation = Reserved.objects.filter(room=self.room).exists() and \
                      Reserved.objects.filter(
        begin_date__gte=datetime.date.today(),
        end_date__lte=datetime.date.today()
    )

    if date_validation:
        raise RuntimeError('You can not reserve this room. Interval')
    super(ReserveRoomForm, self).save(commit)

For example the there are reservation in begin date 23-January and end date 29-January but user trying to reserve 25-January

How to realize it? Thanks)

Jacob
  • 11
  • 4
  • have look at this [answer](https://stackoverflow.com/a/7366731/5949840), i think this is similar to your query. – Sumeet Kumar Jan 25 '18 at 16:36
  • are you sure that your query is working?. You should use datetime.datetime.now() instead of datetime.date.today() because your fields are datetimefield instead of DateField – Mauricio Cortazar Jan 25 '18 at 17:29

0 Answers0