I'm following the method used by @Yauhen Yakimovich in this question:
do properties work on django model fields?
To have a model field that is a calculation of a different model.
The Problem:
FieldError: Cannot resolve keyword 'rating' into field. Choices are: _rating
The rating
model field inst correctly hidden and overridden by my rating
property causing an error when I try to access it.
My model:
class Restaurant(models.Model):
...
...
@property
def rating(self):
from django.db.models import Avg
return Review.objects.filter(restaurant=self.id).aggregate(Avg('rating'))['rating__avg']
Model in Yauhen's answer:
class MyModel(models.Model):
__foo = models.CharField(max_length = 20, db_column='foo')
bar = models.CharField(max_length = 20)
@property
def foo(self):
if self.bar:
return self.bar
else:
return self.__foo
@foo.setter
def foo(self, value):
self.__foo = value
Any ideas on how to correctly hid the rating
field and define the @property
technique?