I was trying to add daterangefilter in my admin.
below my model
from django.db import models
from order.models import Order, OrderItem
from payment.models import PaymentDetails
from plan import constants as plan_constant
class SubjectPlanManager(models.Manager):
def get_queryset(self):
# Filter for only "subject plan" data (mapped to 2)
return super().get_queryset().filter(plan__plan_type=plan_constant.PLAN_TYPE_MAPPING["subject plan"])
class SalesDashboardOrderItem(OrderItem):
objects = SubjectPlanManager()
class Meta:
proxy = True
verbose_name = "Sales Dashboard Item"
verbose_name_plural = "Sales Dashboard Items"
@property
def payment_details(self):
# Get the PaymentDetails related to this OrderItem
return PaymentDetails.objects.filter(order_id=self.order).first()
@property
def transaction_id(self):
# Return the transaction ID from PaymentDetails
payment = self.payment_details
return payment.transaction_id if payment else "N/A"
@property
def payment_status(self):
# Return the payment status from PaymentDetails
payment = self.payment_details
return payment.state if payment else "N/A"
@property
def payment_amount(self):
# Return the payment amount from PaymentDetails
payment = self.payment_details
return payment.amount if payment else 0
below my admin model
from django.contrib import admin
from django.utils import timezone
from sales_dashboard.models import SalesDashboardOrderItem
from plan import constants as plan_constant
from django.utils.translation import gettext_lazy as _
from rangefilter.filter import DateRangeFilter
from datetime import datetime
from dateutil import tz
from django.utils import timezone
from medway.log import Log
logger = Log().get_default_logger()
class SalesDashboardOrderItemAdmin(admin.ModelAdmin):
list_display = (
'plan',
'order',
'price',
'payment_amount'
)
# Optional: Add filters for better usability
list_filter = (
('plan_start_date', admin.DateFieldListFilter),
('plan_start_date', DateRangeFilter),
'plan',
)
def get_queryset(self, request):
queryset = super().get_queryset(request)
return queryset.filter(plan__plan_type=plan_constant.PLAN_TYPE_MAPPING["subject plan"])
def get_plan_start_date(self, obj):
logger.debug("get_plan_start_date called with obj: %s", obj)
if obj.plan_start_date:
if timezone.is_naive(obj.plan_start_date):
aware_dt = timezone.make_aware(obj.plan_start_date, timezone.get_default_timezone())
else:
aware_dt = obj.plan_start_date
return timezone.localtime(aware_dt)
return None
get_plan_start_date.short_description = _('Plan Start Date')
admin.site.register(SalesDashboardOrderItem, SalesDashboardOrderItemAdmin)
Error:
[2024-09-05 09:30:30,826] [ERROR] [40 140113912736568] [Log ID:67c307e574a1432f9dae15f11adc7830] [Phone Number:None] (exception.process_exception:33) Execption on server, Ex: 'zoneinfo.ZoneInfo' object has no attribute 'localize'