F() Expressions

F() Expressions ডেটাবেজ থেকে ভ্যালু প্যারামিটার হিসাবে নিয়ে ডেটাবেজের সাথে বিভিন্ন অপারেশন করতে সাহায্য করে। মূলত আপডেট এবং ফিল্টার করার জন্য ব্যবহার করা হয়।

from django.db import models

class Product(models.Model):
    name = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    quantity = models.PositiveIntegerField()
    discounted_price = models.DecimalField(max_digits=10, decimal_places=2)

আপডেট করার জন্য

সমস্ত price কে দুই দ্বারা গুন করলাম।

from django.db.models import F

# Double the price of all products
Product.objects.update(price=F('price') * 2)

ফিল্টার করার জন্য

from django.db.models import F

# Retrieve products where the regular price is greater than the discounted price
products = Product.objects.filter(price__gt=F('discounted_price'))

Last updated