Lighthouse
Lighthouse

Reputation: 27

Changing from CharField to IntegerField - Postgres Column data type hasn't changed Django

So I switched from sqlite to postgresql for production of my django reviews website- but due to psql being strongly typed, I'm having errors with my code

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.

So I figured I could change the type by changing my CharFields to IntegerFields to change the type to int... like so

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

to

    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)

with choices:

class Review(models.Model):
    RATING_CHOICES = (
        (1, '1'),
        (2, '2'),
        (3, '3'),
        (4, '4'),
        (5, '5'),
    )

But then i read somewhere that sql doesn't directly support changing a string column to an int column....So i'm still getting the same error. I tried deleting my db creating it new again, and I'm still getting the same exact error after migrating.

I believe this is the bit that is triggering the error

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

But yeah I'm just trying to display several averages reviews of companies and such, and can't seem to figure it out after trying for a couple days. thanks in advance!

Upvotes: 0

Views: 561

Answers (2)

Lighthouse
Lighthouse

Reputation: 27

just added int() as manuel said!

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

Upvotes: 0

Manuel Carrero
Manuel Carrero

Reputation: 637

You have a cast problem, so first you need to answer, ¿What is the data type that you really need? If choose CharField, you can cast your fields directly in the query, just like this:

Avg(int('your_field'))

Upvotes: 1

Related Questions