How to implement this logic in Django models?

class Scheme(models.Model):
class Type(models.TextChoices):

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

I would consider doing something like this:

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 =
        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 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.

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

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.