因此,我从sqlite切换到了postgresql来制作django评论网站,但是由于psql的类型过于严格,我的代码出现了错误
LINE 1: ...Col1, (AVG(((("boards_review"."move_in_condition" + "boards_...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
所以我认为我可以通过将CharFields更改为IntegerFields来将类型更改为int来更改类型...
move_in_condition = models.CharField(max_length=5,choices=RATING_CHOICES, blank=False, default='5')
treatment = models.CharField(max_length=5, choices=RATING_CHOICES, blank=False, default ="5")
response_speed = models.CharField(max_length=5, choices=RATING_CHOICES, blank=False, default ="5")
maintenance_quality = models.CharField(max_length=5, choices=RATING_CHOICES, blank=False, default ="5")
到
move_in_condition = models.IntegerField(choices=RATING_CHOICES, blank=False, default=5)
treatment = models.IntegerField(choices=RATING_CHOICES, blank=False, default =5)
response_speed = models.IntegerField(choices=RATING_CHOICES, blank=False, default =5)
maintenance_quality = models.IntegerField(choices=RATING_CHOICES, blank=False, default =5)
有选择:
class Review(models.Model):
RATING_CHOICES = (
(1, '1'),
(2, '2'),
(3, '3'),
(4, '4'),
(5, '5'),
)
但是后来我读到某处sql不直接支持将字符串列更改为int列...。所以我仍然遇到相同的错误。我尝试删除我的数据库,再次创建它为new,但是在迁移之后,我仍然收到相同的确切错误。
我相信这是引发错误的地方
def get_avg(self):
return self.reviews.annotate(
overall_rating = Sum(
F('move_in_condition') +
F('treatment') +
F('response_speed') +
F('maintenance_quality')
)/4).aggregate(
Avg('overall_rating'),
Avg('move_in_condition'),
Avg('treatment'),
Avg('response_speed'),
Avg('maintenance_quality')
)
但是,是的,我只是想展示一些公司等的平均评价,并且经过几天的尝试似乎无法弄清楚。预先感谢!
答案 0 :(得分:1)
您有一个强制转换问题,因此首先需要回答“您真正需要的数据类型是什么?如果选择CharField,则可以像下面这样直接在查询中转换字段:
write(sockfd, "ABCDEFGH\n", 9);
答案 1 :(得分:0)
就像manuel所说的那样添加int()!
def get_avg(self):
return self.reviews.annotate(
overall_rating = Sum(int(
F('move_in_condition') +
F('treatment') +
F('response_speed') +
F('maintenance_quality')
))/4).aggregate(
Avg('overall_rating'),
Avg('move_in_condition'),
Avg('treatment'),
Avg('response_speed'),
Avg('maintenance_quality')
)