我有模型(对象),其中包含一些字段+ 评分:
class Object(PolymorphicModel):
author = models.ForeignKey(ProfileUser, on_delete=models.CASCADE, db_column='author')
title = models.CharField(max_length=300)
city = models.ForeignKey(City, on_delete=models.CASCADE)
address = models.CharField(max_length=300)
phone = models.CharField(max_length=20, default='')
email = models.CharField(max_length=100, default='')
site = models.CharField(max_length=100, default='')
facebook = models.CharField(max_length=100, default='')
instagram = models.CharField(max_length=100, default='')
content = models.TextField()
rating = models.IntegerField(default=10)
created_date = models.DateTimeField(default=timezone.now)
approved_object = models.BooleanField(default=False)
admin_seen = models.BooleanField(default=False)
def __str__(self):
return f"{self.title}"
这些对象还具有评论模型:
class Comment(models.Model):
object = models.ForeignKey(Object, on_delete=models.CASCADE, related_name='comments')
author = models.ForeignKey(ProfileUser, on_delete=models.CASCADE, db_column='author')
content = models.TextField()
rating = models.TextField()
approved_object = models.BooleanField(default=False)
admin_seen = models.BooleanField(default=False)
created_date = models.DateTimeField(default=timezone.now)
def __str__(self):
return f"{self.content}"
好了,这是笔交易,我有approved_object
和admin_seen
,它们在Django Admin中的单选按钮中都表示出来。我想在某些管理员打开未批准的评论并检查admin_seen
和approved_object
单选按钮时,脚本应使用Comment.objects.filter(object_id=pk).aggregate(Avg('rating'))['rating__avg']
更新评分和平均评分,所以我发现了用def save_method
重写保存方法,但是我不知道如何处理是否选中单选按钮并更新等级?
admin.py :
class CommentScreenAdmin(admin.ModelAdmin):
raw_id_fields = ('author', 'object')
def save_model(self, request, obj, form, change):
#to do...