0

I am writing a Django web app and I'm having a problem with writing test method for my model and I hope you will be able to help me with it.

I created a model:

class Animal(models.Model):
    date_born = models.DateTimeField('date_born', validators=[MaxValueValidator(limit_value=timezone.now())])

New Animal instance should not be created if date_born is set to future date. I am trying to write a test in tests.py to validate if model with future date is not being created but I am not sure how to do it. Could you advise me?

  • This should help you: https://stackoverflow.com/questions/3642892/calculating-if-date-is-in-start-future-or-present-in-python/3642942 – FLAK-ZOSO Jan 23 '22 at 19:22

1 Answers1

0

from what i am guessing from your question, the problem is that you want to make sure the value of date_born is always the current time, if so then in that case there is a auto_now_add keyword argument in django datetime model and it recieves a boolean. It's default to False and can be set to True and django will take care of the rest.