How do I make two choice fields that will be saved as one value in the database in django?

Viewed 26

I have created a Task model:

class Task(models.Model):
    name = models.CharField(max_length=200, null=True)
    author = models.ForeignKey(User, on_delete=models.DO_NOTHING, blank=True, null=True)
    company = models.ForeignKey(Company, on_delete=models.DO_NOTHING)
    eta = models.CharField(max_length=20, null=True, blank=True)

I want the eta to be saved as a CharField that will be inputed by the user via two fields: one field for the measurement, for example: months, days, minutes etc. and one field for the amount, for example: 1, 4, 10 etc...

at the end I would like to have the eta field be '1 hour', '30 minutes', '2 weeks' etc...

I want the user to input the fields and then have the backend save them in to the right format.

can anyone help with how to do this? or maybe if someone has an idea on how to do this in a better way. I want to make an application that will detect when the ETA already passed and alert the user of that.

1 Answers

You should use a DateField for this functionality with:

eta = models.DateField(auto_now=False, auto_now_add=False)

When you render this field using a Model Form, it will display a Date Input widget for the user to select a date when the 'eta' is expected.

The countdown to 'eta' should be done separately to storage of the expected date, perhaps with some JavaScript. I have linked some resources below.

Model field reference: https://docs.djangoproject.com/en/4.1/ref/models/fields/#datefield

Widget: https://docs.djangoproject.com/en/4.1/ref/forms/widgets/#django.forms.DateInput

Countdown functionality:

https://www.w3schools.com/howto/howto_js_countdown.asp

How to add a countdown timer as a user field in django?

How to implement countdown timer in django

Related