Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从CharField更改为IntegerField-Postgres列数据类型';t改变了Django_Python_Django_Postgresql_Sqldatatypes_Django Migrations - Fatal编程技术网

Python 从CharField更改为IntegerField-Postgres列数据类型';t改变了Django

Python 从CharField更改为IntegerField-Postgres列数据类型';t改变了Django,python,django,postgresql,sqldatatypes,django-migrations,Python,Django,Postgresql,Sqldatatypes,Django Migrations,所以我从sqlite切换到postgresql来制作我的django评论网站——但由于psql是强类型的,我的代码有错误 LINE 1: ...Col1, (AVG(((("boards_review"."move_in_condition" + "boards_... ^ HINT: No operator matches the given name and argum

所以我从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")

有选择:

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')
            )

但是,是的,我只是想展示一些公司的评论,在尝试了几天之后,我似乎无法理解。提前谢谢

您有一个cast问题,因此首先需要回答:?您真正需要的数据类型是什么?如果选择CharField,则可以直接在查询中强制转换字段,如下所示:

Avg(int('your_field'))
正如曼努埃尔所说,刚刚添加了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')
            )

您是否尝试过直接在查询中强制转换字段?就像这样:
Avg(int('your_field'))
这很有效!谢谢你…你在哪里添加这个。。。显示内联代码。您可能需要使用“…代码片段”。。只显示代码的短暂和平,但请记住,人们可以按照代码的位置进行操作。(审查结束)。
    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')
            )