[Solved] I need to do a function that calculates the item prices dynamically without storing them in the database django

Mohamed Chougrani Asks: I need to do a function that calculates the item prices dynamically without storing them in the database django
I need to do a function which calculates the prices of the items dynamically without storing them in the database because the prices of the items change daily, I have scratched the price of the day and I need the items to be calculated based on this price


class Product(models.Model):
    prdct_name = models.CharField(max_length=17, verbose_name=_("Article "))
    prdct_category = models.ForeignKey("Category", on_delete=models.CASCADE, blank=True, null=True)
    prdct_description = models.TextField(verbose_name=_("Description"))
    prdct_img = models.ImageField(upload_to='product/',verbose_name=_("Image"), blank=True, null=True)
    # prdct_img
    prdct_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name=_("Prix "))
    prdct_cost = models.DecimalField(max_digits=10, decimal_places=2, verbose_name=_("Prix promotionel "))
    prdct_weight = models.DecimalField(max_digits=10, decimal_places=2, verbose_name=_("Poid "))
    prdct_created = models.DateTimeField(auto_now=False, auto_now_add=False, verbose_name=_("Date Creation "))
    prdct_genre = models.CharField(max_length=50, verbose_name=_("Genre "))
    prdct_matiere = models.CharField(max_length=50, verbose_name=_("Matiere "))
    prdct_titrage = models.CharField(max_length=50, verbose_name=_("titrage "))
    prdct_in_stock = models.BooleanField(verbose_name=_("in_stock "))
    prdct_slug = models.SlugField(blank=True, null=True)

the function to scrape the price from the site

def get_prixmatiere():
    # get price from cpor
    import requests
    from bs4 import BeautifulSoup
    response = requests.get("https://www.gold.fr/cours-or-prix-de-l-or/")
    soup = BeautifulSoup(response.text , 'lxml')
    product_price = soup.find_all("td",{"class" : "price"})[0].text.replace('€', '').replace(' ', '')
    # calculate price
    prixdug = float(product_price) / 4 * 3 / 1000 * 210 - 500
    prixmatiere = float(product_price) / 4 * 3 / 1000 * 210
    return prixmatiere

i need to get the price of the item

def calc_price_item(self):
    self.dailyprice = get_prixmatiere()
    item_price = self.prdct_price + self.dailyprice * self.prdct_weight
    return item_price

and then display it


from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
from .models import Product, ProductImage, Category
from django.core.paginator import Paginator

# function get all product
def product_list(request):
    product_list = Product.objects.all()[:5]        # get all product in variable le model doit etre importe depuis la class

    context = {
        'product_list' : product_list,       # contexte dictionaire variable qui contien les produit = product_list pour lutuliser dans les loupe dans le views

    return render(request, 'Product/product_list.html', context)  # retourne la requete + le nom du dossier(dans le templates) + context

def product_detail(request, slug, id):
    product_detail = Product.objects.get(prdct_slug=slug)
    images = ProductImage.objects.filter(prdct_product_id=id)
    cat_parent = Category.objects.filter(level='0')

    context = {'product_detail' : product_detail,
                'images' : images,
                'cat_parent': cat_parent,

    return render(request, 'Product/product_detail.html', context)

def category_product(request, id, slug):
    cat_slug = Category.objects.get(cat_slug=slug)
    category_product =  Category.objects.filter(parent_id=id)
    product = Product.objects.filter(prdct_category=id)

    paginator = Paginator(product, 20) # Show 20 contacts per page.

    page_number = request.GET.get('page')
    product = paginator.get_page(page_number)

    context = {

            'id' : id,
            'cat_slug' : cat_slug,
            'category_product': category_product,
            'product': product,


    return render(request, 'Product/category_product.html', context)

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.