Sunday, June 27, 2021

Re: Manager.raw() in a class method

Not actually sure what the issue is here, but I'm a devotee of avoiding raw sql at any stage. :-)

If your query is really this simple, you certainly don't need a raw query to achieve it, nor a SQL proc to handle the sub-query that results from it.

Either way, bear in mind that all that is returned here is a queryset and not the results.

On Wed, Jun 23, 2021 at 6:45 PM Daniel Sears <daniel.sears@gmail.com> wrote:
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.

--
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/CAE5VhgW2%2BjgC_ZH0WpkchhbnugW-Ya5u9Mqzj0%3DTXFtF_6bVCg%40mail.gmail.com.

No comments:

Post a Comment