some advices for Query method split_exclude

44 views
Skip to first unread message

汪晟

unread,
Sep 5, 2020, 1:33:27 PM9/5/20
to Django developers (Contributions to Django itself)
Hello Mentors and contributors, I’m a  web site developer , when I use Django . I custom a  class < SomeQuery > base on < Query >, and modify it’s attribute ‘compiler' to  Another one <OtherSQLCompler> which custom by myself,. But when I use  < SomeQuery > in a < Queryset > object, and call the method filter() with a params ~Q() ,it will call  the method ‘split_exclude’ in  < Query >,
In the line 1772 in the file <django/db/models/sql/query.py> 

# Generate the inner query.
query = Query(self.model)

The code instance a object by the base class < Query >,  but I want it instance a object base on < SomeQuery > 

So why we don’t make the code like this query = self.__class__(self.model)

I think that is the correct way .

Adam Johnson

unread,
Sep 5, 2020, 2:12:53 PM9/5/20
to django-d...@googlegroups.com
Hi 汪晟,

Extending the ORM with a subclass of Query isn't common, and not documented. Perhaps for extensibility this line of code should use "self.__class__" but there aren't any guarantees about other parts of the ORM.

The extensions I created for MySQL only involve the QuerySet layer: https://django-mysql.readthedocs.io/en/latest/queryset_extensions.html . Perhaps you can track your customization within a custom QuerySet, and only "at the last moment", before execution, swap the Query object's class and compiler. From what I can tell, this should be possible in an iterator() method, before calling super().

It would help to know your use case. It's possible whatever extension you're adding can be done outside of Query or Compiler. For an example using database instrumentation, see my recent post: https://adamj.eu/tech/2020/07/29/backporting-a-django-orm-feature-with-database-instrumentation/

Thanks,

Adam

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/93ef71bd-3fe3-4412-9c1c-886f88cbfaaan%40googlegroups.com.


--
Adam
Reply all
Reply to author
Forward
0 new messages