Apologies if this is a stupid question or if my terminology is not quite right. I’m very new to Django and programming!
I’m looking to build a quoting tool for my website, which will basically be a form that clients fill in, which will create an instance of a “quote” model, and the actual quote value will be automatically calculated and stored as part of the model instance, based on the data input by the user.
I’ve looked around online but I’m really struggling to find a simple explanation of how a model field can be validated and/or automatically calculated and stored, based on data inputs from earlier fields.
My first hurdle is below:
class Quote(models.Model):
total_plots = models.IntegerField(default=1)
total_types = models.IntegerField(default=(total_plots),validators=[MinValueValidator(1),MaxValueValidator(total_plots)])
So what I’m trying and failing to do here is initially set the default value for total_types, to the same as what the user has input for total_plots. I want to then allow the user to change the value of total_types, but it can’t be greater than what they have entered for total_plots, so I need to grab the value they enter for total_plots and use it to restrict what they are able to enter for total_types.
The error I’m getting here is this: “TypeError: ‘<=’ not supported between instances of ‘IntegerField’ and ‘int'”
Further down the line, I will also want the quote value to be calculated automatically, and stored as part of the instance of the model.
I’m imagining that part of the model to look something like:
quote_value = total_time * hourly_rate
or
quote_value = self.total_time * self.hourly_rate
#Where total time has also been automatically calculated based on a plethora of other user inputs, and hourly rate will be a variable that I can set/adjust.
Again I will need to grab a lot of data from user inputs and use it to calculate a result, and then store those results as part of the model instance.
user26048173 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.