0

I'm using Django 2.0

I have a model class

class AmountGiven(models.Model):
    amount = models.FloatField(help_text='Amount given to the contact')
    _given_date = models.DateTimeField(
        db_column='given_date',
        default=datetime.now,
        help_text='Date and time when amount was given to the contact'
    )
    return_date = models.DateTimeField(
        blank=True,
        default=None,
        null=True
    )
    modified = models.DateTimeField(auto_now_add=True)
    created = models.DateTimeField(auto_now=True)

    def __str__(self):
        return str(self.amount)

    @property
    def given_date(self):
        return self._given_date

    @given_date.setter
    def given_date(self, value):
        value_2 = value

        try:
            value_2 = datetime.strptime(value_2, "%Y-%m-%d %H:%M:%S").date()
        except ValueError:
            try:
                datetime.combine(value, datetime.min.time())
                value_2 = datetime.strptime(value_2, "%Y-%m-%d").date()
            except ValueError:
                raise ValueError("Date is not of valid format. %Y-%m-%d %H:%M:%s")

        if value_2 > datetime.today().date():
            raise ValueError("The date chosen was in the future.")
        self._given_date = value

I want to validate if given_date is not in future. If given_date is in future, it will raise a validation error and not save the form data.

In pytest, I'm doing

@pytest.mark.django_db
class TestAmountGiven(TestCase):
    def test_model_add_amount_given(self):

        amount_given = AmountGiven(
            amount=10000.00,
            given_date='2018-1-30 00:00:00',
            promised_return_date='2019-02-01 00:00:00'
        )
        amount_given.save()

But this is giving warning as

transactions/tests/test_models.py::TestAmountGiven::test_model_add_amount_given
  /path_to/python3.6/site-packages/django/db/models/fields/__init__.py:1423: 
        RuntimeWarning: DateTimeField AmountGiven._given_date received a naive datetime (2018-01-30 00:00:00) while time zone support is active.
    RuntimeWarning)
  /path_to/python3.6/site-packages/django/db/models/fields/__init__.py:1423: 
        RuntimeWarning: DateTimeField AmountGiven.promised_return_date received a naive datetime (2019-02-01 00:00:00) while time zone support is active.
    RuntimeWarning)

1. Is this the right approach of writing Field validation? (using setter)
2. How to resolve the warning message

Anuj TBE
  • 9,198
  • 27
  • 136
  • 285
  • For sure, is good to handle this backend, but why not altering the front-end and not allowing such input to be sent to the back-end? – Rafael Jul 08 '18 at 12:26
  • Have you looked at the validator docs? https://docs.djangoproject.com/en/2.0/ref/validators/ – thebjorn Jul 08 '18 at 12:35
  • Take a look at https://stackoverflow.com/questions/18622007/runtimewarning-datetimefield-received-a-naive-datetime for resolving your warning message. – thebjorn Jul 08 '18 at 12:49
  • 1
    @Rafael Validation will be on front-end side, but to be on safe side always I want validation in back-end also so that in case of validation missing in front-end it should return validation error message. – Anuj TBE Jul 08 '18 at 13:01

0 Answers0