从 CharField 更改为 IntegerField - Postgres 列数据类型未更改 Django
Changing from CharField to IntegerField - Postgres Column data type hasn't changed Django
所以我从 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 列....所以我仍然遇到同样的错误。我尝试删除我的数据库并再次创建它,但迁移后我仍然遇到完全相同的错误。
我相信这是触发错误的位
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')
)
但是,是的,我只是想显示一些对公司等的平均评价,尝试了几天后似乎无法弄清楚。提前致谢!
您遇到了转换问题,所以首先您需要回答,¿您真正需要的数据类型是什么?如果选择 CharField,您可以直接在查询中转换您的字段,就像这样:
Avg(int('your_field'))
刚刚添加了 int(),正如 manuel 所说!
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')
)
所以我从 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 列....所以我仍然遇到同样的错误。我尝试删除我的数据库并再次创建它,但迁移后我仍然遇到完全相同的错误。
我相信这是触发错误的位
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')
)
但是,是的,我只是想显示一些对公司等的平均评价,尝试了几天后似乎无法弄清楚。提前致谢!
您遇到了转换问题,所以首先您需要回答,¿您真正需要的数据类型是什么?如果选择 CharField,您可以直接在查询中转换您的字段,就像这样:
Avg(int('your_field'))
刚刚添加了 int(),正如 manuel 所说!
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')
)