lundi 28 décembre 2020

Show values based on dropdown selection on the same page

Is there a way for me to show the values based on selection of dropdown using if statements in Django.

The pricing output value depend on the contract selected and if it is a half day or full day, if it is a half day I just take the outputted price and divide by 2. Below is my model and form, I have been told that I need to do this ideally in the model but cannot figure out the code for a method to work this out or if I need to do it in the template or view, keep hitting brick walls if you have any documents you recommend I should read please let me know.

The way I am looking at solving this is that the dropdown lets say 1-Year Weekly will take the day rate x the number of days until the end date based on:

   @property
   def day_rate_year1(self):
     return self.year1 / weeksinyear / 5

Ideally I would like to have the results of the dropdown shown on the same pages, as I plan to use another button to generate a PDF. I am guessing from my research that this would be done using AJAX

Model

    class AdminData(models.Model):
        year1 = models.IntegerField()
        year3 = models.IntegerField()
        year1_fortnight = models.IntegerField()
        year3_fortnight = models.IntegerField()

    @property
    def fortnight_dayrate_year1(self):
        return self.year1_fortnight / weeksinyear / 5

    @property
    def fortnight_dayrate_year3(self):
        return self.year3_fortnight / weeksinyear / 5

    @property
    def day_rate_year1(self):
        return self.year1 / weeksinyear / 5

    @property
    def day_rate_year3(self):
        return self.year3 / weeksinyear / 5


class Price(models.Model):
        year_choice = Choices('1-Year Weekly', '3-Year Weekly','1-Year Fortnightly', '3-Year 
        Fortnightly')
        day_choice = Choices('Full Day', 'Half Day')
        name = models.CharField(max_length=100)
        contract = StatusField(choices_name='year_choice')
        time_daily = StatusField(choices_name='day_choice')
        start_date = models.DateField(default=datetime.now)
        end_date = models.DateField(default=datetime(2021,3,31))
        epoch_year = date.today().year
        year_start = date(epoch_year, 1, 4)
        year_end = date(epoch_year, 3 , 31)


        def __str__(self):
            return self.name

Please let me know if you need further code.

Thanks for the help, sorry for the stupid question.

Aucun commentaire:

Enregistrer un commentaire