告诉我,我是否正确解决了问题?
这是我的解决方案
在models.py
class UserRoleMeta(models.Model):
TYPEMETA = (
...
('review', 'Отзывы'),
)
parents = models.ManyToManyField("self", blank=True, related_name="parents_list")
type = models.CharField(max_length=10, choices=TYPEMETA, default='cat', verbose_name='Тип мета')
rating = models.DecimalField(...)
def save(self, commit=False, *args, **kwargs):
super().save(*args, **kwargs)
self.save_m2m(*args, **kwargs)
def save_m2m(self, *args, **kwargs):
if self.type == 'review':
for parent in self.parents.all():
rating = UserRoleMeta.objects.filter(parents=parent).aggregate(average=Avg('rating'))
parent.rating = rating['average']
parent.save()
super().save(*args, **kwargs)
它有效,但是我担心该决定的正确性。这里没有多余的东西吗?