1

The query itself is correct and I am able to get the raw query set. I need to convert this into query set for further processing and I am facing below error.

Creating corresponding django query was hard for me and that is why I created SQL query, got the raw query set and now attempting to convert it to query set for further processing.

I have changed model and table names for anonymity.

queryset = Test1.objects.filter(id__in=RawSQL("SELECT DISTINCT ON (test1.start_time, test1.id) test1.id, test1.name, test1.start_time FROM test1 WHERE EXISTS (SELECT * FROM test2 JOIN test3 ON test2.test3_id = test3.id AND test3.value = '{param}' JOIN test4 ON test2.test4_id = test4.id AND test4.test1_id = test1.id) ORDER BY test1.start_time DESC".format(param=val)))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: __init__() missing 1 required positional argument: 'params'

Here is the formatted raw query

SELECT
  DISTINCT ON (test1.start_time, test1.id) test1.id,
  test1.name,
  test1.start_time
FROM
  test1
WHERE
  EXISTS (
    SELECT
      *
    FROM
      test2
      JOIN test3 ON test2.test3_id = test3.id
      AND test3.value = '{param}'
      JOIN test4 ON test2.test4_id = test4.id
      AND test4.test1_id = test1.id
  )
ORDER BY
  test1.start_time DESC.format(param = val)
)
  • How are you passing the value for {param}? I can see the SQL query needs that parameter value, but I don't see where it is supposed to come from. Can you share the rest of your code? – Aron Apr 06 '21 at 18:37
  • val = '1.2.3.4' and that is used to set the param. – Sandesh Veerapur Apr 06 '21 at 19:45
  • val = '1.2.3.4'? do you mean param = '1.2.3.4'? the error says you aren't instantiating the variable and it can't be found but you haven't actually shared any code so I'm not sure where the problem is https://stackoverflow.com/questions/19929626/init-missing-1-required-positional-argument – Aron Apr 07 '21 at 00:48
  • Here is the output from django shell. – Sandesh Veerapur Apr 07 '21 at 21:09

0 Answers0