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)
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