How can I link user input data from one field in a Django model, to another field in the same model?

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.

I think I am understanding what you're trying to do, but I'm not sure I'm understanding all the details of what you've got set up, so this is more of an idea suggestion than an answer.

Something to keep in mind about models is that they represent tables in your database, and not necessarily particular instances of a quote. My way to go about this would be to set each of these fields very simply in the model, and then put any logic for autofill or maximums into the view itself. It will be much easier to validate data in the view than in the model, because you will be able to deal with specific instances/quotes, rather than the abstract idea of what a quote should look hypothetically look like.

It might also be useful (on the models side) to set up multiple models so that you can break things down into as individual pieces as possible, and then use things like foreign keys and joins to combine those smaller pieces into a more useful whole. This idea is called Database Normalization and can be very helpful for setting up your models in a way that makes them easier to use.

Also, welcome to Django programming, congrats on starting the journey! If any part of my answer was confusing let me know and I can give more explanation.

Вернуться на верх