So guys i have been trying to use the ExpressionWrapper in django and get outputfield as a IntegerField(), I don’t know where I am going wrong
The imports
# Imports from django.core
from attr import fields
from django.db import models
from django.db.models import DecimalField, IntegerField, Avg,Subquery,OuterRef, Q, Sum, F, ExpressionWrapper
from django.db.models.functions import ExtractYear
from django.contrib.auth.models import User
from django.forms import DateField, DecimalField, IntegerField
from django.shortcuts import get_object_or_404
from django.utils import timezone
# Imports froms apps
from shop.models import CartItem, Order, OrderItem, Product, Review
from wallets.models import Transaction, Wallet
This is code part of a model method
Profile.objects.filter(
id__in = Wallet.objects.filter(
id__in=purchases_made_for_on_store_products.values('wallet')
).values('user')
).values('dob').annotate(
age = ExpressionWrapper(ExtractYear(dt.today()) - ExtractYear(F('dob')),
output_field=IntegerField()))
)
I was expecting the code to calculate the age of each of each user and store it temporarily in in a field called “age”. this would allow me to annotate it and further used for filtering.