Closed shuki25 closed 3 years ago
Josh, since AjaxDatatableView uses so many properties of the QuerySet for filtering, sorting ... and so on, this doesn't seem an easy task. I have little experience with RawQuerySet and can't figure out an easy solution to suggest.
However, let me describe a possible alternative approach.
(1) You could save your complex SQL statements in a DB View:
DROP VIEW IF EXISTS "reports_whatever";
CREATE VIEW "reports_whatever" AS
... sql statement here ...
The above script could be kept in you repo, and run multiple times to recreate the View any time your SQL changes; conceptually: at deployment time.
(2) In models.py, define a Model with a list of fields which mimics the columns produced by the previous VIEW:
class Whatever(models.Model):
class Meta:
managed = False
... fields ...
The important detail above is managed = False
, which excludes this model from Django's migrations.
(3) With this setup, Whatever
exposes a proper QuerySet, still delegating data extraction to you complex SQL statement.
Actually using views would work. Why didn't I think of that in the first place? lol
Thanks for your response!
you're welcome ;)
Would it be possible to add support for RawQuerySet data type if we use raw SQL instead of Django's OMS? For example Manager.objects.raw_as_qs("select * from mytable")? My project has complex SQL statements for report generation and would like to be able to use raw SQL.
For example, code snippet below:
And for the ajax call:
Here's the traceback:
I don't think it would work well when converting RawQuerySet to QuerySet, it just introduces new problems. I'm thinking we may have to create a separate class to deal with raw SQL. I like your package and would love to use it with raw SQL as well.
I could make a PR to support that if you could direct me to the best starting point.
Cheers, Josh