# Auto populate a date field when a certain choice is made in choices

**URL:** <https://forum.djangoproject.com/t/auto-populate-a-date-field-when-a-certain-choice-is-made-in-choices/18237>\
**Category:** Forms & APIs\
**Created:** [January 17, 2023, 4:39am UTC](https://forum.djangoproject.com/t/auto-populate-a-date-field-when-a-certain-choice-is-made-in-choices/18237 "2023-01-17T04:39:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![matteyahmed](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/matteyahmed/32/11483_2.png) [@matteyahmed](https://forum.djangoproject.com/u/matteyahmed)\
**Post date:** [January 17, 2023, 4:39am UTC](https://forum.djangoproject.com/t/auto-populate-a-date-field-when-a-certain-choice-is-made-in-choices/18237/1 "2023-01-17T04:39:02Z")

</div>

I’m trying to auto populate (update) a datefield, when a certain choice is chosen by the user:  
following is my code

```auto
class Tasks(models.Model):
    name = models.CharField(max_length=255)
    boat = models.ForeignKey(Boat, on_delete=models.CASCADE, null=True, blank=True)
    assigned_to = models.ForeignKey(Technician, on_delete=models.SET_NULL, null=True, blank=True)
    created_by = models.ForeignKey(Person, on_delete=models.SET_NULL, null=True, blank=True)
    created_date = models.DateTimeField(default=datetime.now, blank=True)
    completed_date = models.DateField(null=True, blank=True)

    CONDITION_STATUS = (
        ('c', 'Completed'),
        ('p', 'Pending'),
        ('i', 'Incomplete'),
    )

    status = models.CharField(
        max_length=1,
        choices=CONDITION_STATUS,
        blank=True,
        default='i',
    )

```

as you can see, I want, when a `technicain` choses `completed` choice, it to automatically trigger an insert on datetime to the `completed_date` field.

I’m trying this with a clean method in my forms.modelForm class:

```auto
class TaskForm(forms.ModelForm):

    class Meta:
        model = Task

        fields = ( 
            'created_by',
            'created_date',
            'completed_date',
	**....
            )

        def clean(self):
            cleaned_data = super().clean()
            if self.errors:
                return cleaned_data
            choice = cleaned_data.get('status')
            if choice == 'c':
                cleaned_data['completed_date'] = datetime.date.today()
            return cleaned_data
	
	**....

```

however it seems it is not working, and what is the best approach to it, or can point me to the right direction

thanks.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 17, 2023, 5:32am UTC](https://forum.djangoproject.com/t/auto-populate-a-date-field-when-a-certain-choice-is-made-in-choices/18237/2 "2023-01-17T05:32:35Z")

</div>

You’re not showing a status field in your form. There doesn’t appear to be anything to check.

---

<div class="post-metadata">

**Author:** ![matteyahmed](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/matteyahmed/32/11483_2.png) [@matteyahmed](https://forum.djangoproject.com/u/matteyahmed)\
**Post date:** [January 17, 2023, 5:47am UTC](https://forum.djangoproject.com/t/auto-populate-a-date-field-when-a-certain-choice-is-made-in-choices/18237/3 "2023-01-17T05:47:47Z")

</div>

I have managed to solve this issue by overriding the save method in my model:

```auto
  def save(self, *args, **kwargs):
        if self.status == 'c':
            self.completed_date = timezone.now()
        super().save(*args, **kwargs)

```

is this django’s best practiced way to accomplish my desired results, or should I introduce the `status` field in to my Form, and use a `clean_data()` method to get my desired result.

thanks

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 17, 2023, 1:00pm UTC](https://forum.djangoproject.com/t/auto-populate-a-date-field-when-a-certain-choice-is-made-in-choices/18237/4 "2023-01-17T13:00:26Z")

</div>

I’m not sure this is what you want either. In this case, _every time_ this model is saved after having the status == ‘c’, the date’s going to be updated. (Whether you want this to happen is only something you can decide.)

---

<div class="post-metadata">

**Author:** ![matteyahmed](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/matteyahmed/32/11483_2.png) [@matteyahmed](https://forum.djangoproject.com/u/matteyahmed)\
**Post date:** [January 17, 2023, 3:56pm UTC](https://forum.djangoproject.com/t/auto-populate-a-date-field-when-a-certain-choice-is-made-in-choices/18237/5 "2023-01-17T15:56:48Z")

</div>

yes, it will get updated. I think i can bypass it through the templates, for now. Thanks.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 17, 2023, 5:04pm UTC](https://forum.djangoproject.com/t/auto-populate-a-date-field-when-a-certain-choice-is-made-in-choices/18237/6 "2023-01-17T17:04:09Z")

</div>

> [@matteyahmed](#):
>
> I think i can bypass it through the templates,

Actually, no. Your templates aren’t going to have any effect on that at all. With that setting in your model, any time a model instance is saved for _any_ reason, when the status is ‘c’, the completed\_date will be updated.

I’m suspecting that what you really want to do is only update the completed\_date the first time the status becomes ‘c’.

---

<div class="post-metadata">

**Author:** ![matteyahmed](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/matteyahmed/32/11483_2.png) [@matteyahmed](https://forum.djangoproject.com/u/matteyahmed)\
**Post date:** [January 18, 2023, 5:51am UTC](https://forum.djangoproject.com/t/auto-populate-a-date-field-when-a-certain-choice-is-made-in-choices/18237/7 "2023-01-18T05:51:03Z")

</div>

I’ve update the logic to :

```auto
    def save(self, *args, **kwargs):
        if self.status == 'c' and self.completed_date is None:
            self.completed_date = timezone.now()
        super().save(*args, **kwargs)

```

so in the first time it will be saved, but if an value exists in the `completed_date ` its value won’t be updated, can share feedback to improve

thanks
