Wednesday, June 23, 2021

Manager.raw() in a class method

I have a SQL function that works when I run it directly from an SQL script and now I'm trying to run it with Manager.raw() in a model method. I wrote a model instance method that worked fine:

    def get_store_products_sql(self) -> list['Product']:
        return Product.objects.raw(
                                     '''SELECT  prod_id AS id,
                                                prod_name,
                                                prod_price
                                          FROM  get_store_products(%s)''',
                                     [self.id]
                                  )

Now I'm trying to write a similar class method:

    @classmethod
    def get_products_by_color_sql(cls, color) -> list['Product']:
        return Product.objects.raw(
                                     '''SELECT  prod_id AS id,
                                                prod_name,
                                                prod_price
                                          FROM  get_products_by_color(%s)''',
                                     [color]
                                  )

This fails pretty silently without even entering get_products_by_color(). color is a legal string and I've also tried simply using "green" without any luck.

As I said, this isn't even getting into the get_products_by_color() function, so I doubt that's the problem. It appears to be either a problem with calling Manager.raw() in a class method or a problem with the color string.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CALLzXtQ2a4wd4O69OSL1jEo1YNAuSQCPTJ%2B2LayqGYJ5kUsOHA%40mail.gmail.com.

No comments:

Post a Comment