Hi everyone, I hope you are fine.
I have two fields with the name of price and discountedPrice in a model.
The thing that I want is to get the values of these two fields on their change to calculate the discount percent from them before the admin clicks on the save button.
I will thank anyone to give me a piece of guidance about this matter.
Hi everyone, I hope you are fine.
To confirm that I understand what you’re asking for -
You have a form being displayed in the browser.
The form has two fields,
When the user updates something (what? Either field?) you want another field on the page to calculate the percentage of those two values and update the page - without having to click a button.
Excuse me, I don’t mean that.
I mean I have an ecommerce website that the admin can see a product model in the database and the product model includes two fields with the names of price and discountedPrice.
I want when the admin types the prices in these two fields, the discount percent be calculated before clicking the save button.
Now can you understand what I mean?
Oh, ok, I understand you now.
You assign the value of the calculation to the field in the model where it’s going to be stored.
It’s no different than assigning any other value to a field.
It would be a lot easier to explain if you posted your model and your view here.
Ok off course, here is the picture of my model in the admin panel:
I want to calculate discount percent and put the percent in the discount percent field when the admin types two values for price and discount price and I want to do this before clicking on the save button and doing this challenge is really important for me.
Which one is it?
And here is my model admin code:
@admin.register(ProductList) class ProductListAdmin(admin.ModelAdmin): list_display = ["name", "assortment", "image", "price", "discounted_price", "created_by"] search_fields = ["name"] list_filter = ["assortment", DiscountPercentFilter] readonly_fields = ["discount_percent", "created_by"] class Media: js =( "admin/js/discount.js") def save_model(self, request, obj, form, change): obj.created_by = request.user super().save_model(request, obj, form, change)
But I have a problem that I want the discount.js file to be loaded when the admin wants to add a product but now discount.js file is also be loaded when showing the queryset of the productList.
Why would that be a problem?
The problem is that the only time that I want the discount.js to be executed is when admin click on the plus or add button to add a product to calculate the discount percent but now the discount.js is also loaded on the page which all of the products are showing.
You’re worried about a non-problem. If your code is listening for the right events to be triggered, then it’s not going to execute on a page that doesn’t fire those events.
Please wait a minute to send you the pictures to tell you better that I mean
For example I have put a simple alert in discount.js.
The two below pictures show that if admin wants to see all of products in the product page this file is loaded and the alert is showing and then the list of all products will be showed that I don’t want to be like this.
The only thing that I want is to loading discount.js when the admin clicks on the add product lists button that is in the right side and left side of this page.
Your problem isn’t that the file is being loaded. The problem is that you have a totally unnecessary alert box being shown at the file level. Do this right and you have no problem.
New related title:
“realtime django model field to calculate and show text before saving”
“I have two fields with the name of price and discountedPrice in a model. The thing that I want is to get the values of these two fields on their change to calculate the discount percent from them and showing the discount percent before the admin clicks on the save button. I will thank you to give me a piece of guidance about this matter.”
It seems to me that this question is addressed above.
What information are you looking for that isn’t already covered?
Dear Mr. Whitesell the previous time that we were chatting to solve the problem I did not come up with the solution that you have told because I have explained my question incorrectly.So please forget the previous details of my question.
Right now I mean I want to have two model fields that when the admin enter a text in one of them, the other field show the realtime text of that, such as this codepen example
Ho can I have this item in django models in the admin panel?
OK that’s right.
So can I use a js file to control the admin page of adding an object ?
You can add a JS file to your admin page, yes. See ModelAdmin asset definitions