CodexBloom - Programming Q&A Platform

Unexpected AttributeError when accessing properties in a custom Django model in Python 3.8

👀 Views: 91 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-12
python-3.x django models properties attributeerror Python

I've been struggling with this for a few days now and could really use some help... I'm experimenting with I'm experimenting with I'm wondering if anyone has experience with I'm working with an `AttributeError` in my Django application when trying to access a property on my custom model. Here's a simplified version of my model: ```python from django.db import models class Product(models.Model): name = models.CharField(max_length=100) price = models.DecimalField(max_digits=10, decimal_places=2) discount_percentage = models.FloatField(default=0.0) @property def discounted_price(self): return self.price * (1 - self.discount_percentage / 100) ``` In my views, I'm trying to access this property after retrieving a product instance: ```python from .models import Product def product_detail(request, product_id): product = Product.objects.get(id=product_id) print(product.discounted_price) ``` However, I'm getting the following behavior: ``` AttributeError: 'Product' object has no attribute 'discounted_price' ``` I verified that the database migration was applied correctly and the model is registered in the admin. I also checked to ensure I was not inadvertently shadowing the `Product` class name elsewhere in my code. I tried running the Django shell and directly accessing the property there, but I get the same behavior. Any suggestions on what might be causing this scenario? I'm using Django 3.1 with Python 3.8. This is part of a larger REST API I'm building. Any ideas what could be causing this? Has anyone dealt with something similar? What's the correct way to implement this? My team is using Python for this CLI tool. Any feedback is welcome!