I would like to know, how to pass one field value to another within a single model. I need to filter Rating
objects to get average rating of a product and I want it to be in ProductStat
with other future statistics.
My attempt at filtering, which won't let me make a migration because of some "Models aren't loaded yet"
appregistry error but if I comment out the avgRating
it works.
class ProductStat(models.Model):
productID = models.ForeignKey('Product')
avgRating = models.IntegerField(
default = Rating.objects.filter(product=productID).aggregate(Avg('rating'))['rating__avg']
)
class Rating(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
product = models.ForeignKey('Product')
rating = models.IntegerField(default = 3)
So my question is: how do I make ProductStat.avgRating
filter ratings by ProductStat.product
?