# How to implement this logic in Django models?

**URL:** <https://forum.djangoproject.com/t/how-to-implement-this-logic-in-django-models/6038>\
**Category:** Using Django\
**Created:** [January 8, 2021, 8:24am UTC](https://forum.djangoproject.com/t/how-to-implement-this-logic-in-django-models/6038 "2021-01-08T08:24:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![futurerahul](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/futurerahul/32/2902_2.png) [@futurerahul](https://forum.djangoproject.com/u/futurerahul)\
**Post date:** [January 8, 2021, 8:24am UTC](https://forum.djangoproject.com/t/how-to-implement-this-logic-in-django-models/6038/1 "2021-01-08T08:24:25Z")

</div>

class Scheme(models.Model):  
class Type(models.TextChoices):  
OPEN = ‘OPEN’, ‘OPEN’  
CLOSE = ‘CLOSE’, ‘CLOSE’

```
if Type.choices == Type.Close:
  {a DateField should appear such as maturity_date}
else
  {no field for maturity_date}
```

---

<div class="post-metadata">

**Author:** ![conor](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/conor/32/3736_2.png) [@conor](https://forum.djangoproject.com/u/conor)\
**Post date:** [January 8, 2021, 10:58am UTC](https://forum.djangoproject.com/t/how-to-implement-this-logic-in-django-models/6038/2 "2021-01-08T10:58:54Z")

</div>

I would consider doing something like this:

```python
from datetime import datetime

class Scheme(models.Model):
    class Types(models.TextChoices):
        OPEN = "OPEN", "OPEN"
        CLOSE = "CLOSE", "CLOSE"

    type = models.CharField(
        max_length=50, choices=Types.choices, default=Types.OPEN
    )
    # We can't have attributes determined by if statements. If a model
    # has an attribute, then it always has that attribute. It can however be None
    # the default value for the below is None
    maturity_date = models.DateField(default=None, null=True, blank=True)

    # this method runs everytime an instance of Scheme is saved
    def save(self, *args, **kwargs):
        if self.type == self.Types.CLOSE and self.maturity_date is not None:
            self.maturity_date = datetime.now()
        return super().save(*args, **kwargs)

```

In essence, we have a model with a datetime field which is by default `None`. When save is called on an instance of `Scheme` , `save()` is run and will check if the instance’s type is `CLOSED` and if its time value is `None`. If these conditions are met, then `maturity_date` will be assigned the value of `datetime.datetime.now()`. If the conditions are not met, then `maturity_date` remains as it was, either `None` or the timestamp of when it was first stamped.

Hope this helps you on your way.

---

<div class="post-metadata">

**Author:** ![futurerahul](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/futurerahul/32/2902_2.png) [@futurerahul](https://forum.djangoproject.com/u/futurerahul)\
**Post date:** [January 11, 2021, 5:52pm UTC](https://forum.djangoproject.com/t/how-to-implement-this-logic-in-django-models/6038/3 "2021-01-11T17:52:17Z")

</div>

Thanks conor,  
This solution seems to be working, but the only thing I am facing now is that maturity\_date field is auto filled with current date when it’s close type. I want to post maturity\_date for close type, instead of datetime.now.

---

<div class="post-metadata">

**Author:** ![conor](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/conor/32/3736_2.png) [@conor](https://forum.djangoproject.com/u/conor)\
**Post date:** [January 11, 2021, 5:55pm UTC](https://forum.djangoproject.com/t/how-to-implement-this-logic-in-django-models/6038/4 "2021-01-11T17:55:58Z")

</div>

You’re welcome. If you want to create the `maturity_date` with a POST request and not have it created automatically then you can remove the `save()` method and just let the base class `save()` method do its thing.
