diff options
| author | Gagaro <gagaro42@gmail.com> | 2022-01-31 15:51:38 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-03-16 09:33:16 +0100 |
| commit | bf524d229f3c1008f41450e2750b85395aa75fe6 (patch) | |
| tree | 9ba097a0230f9fd818ca07907fcdb2429b9221a1 /django/db/models/sql/query.py | |
| parent | 970f5bf5035d8c99b0ff883f984b60cf531dddbe (diff) | |
Refs #30581 -- Allowed sql.Query to be used without model.
Diffstat (limited to 'django/db/models/sql/query.py')
| -rw-r--r-- | django/db/models/sql/query.py | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 31a66cec46..54f3258eac 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -46,6 +46,8 @@ __all__ = ["Query", "RawQuery"] def get_field_names_from_opts(opts): + if opts is None: + return set() return set( chain.from_iterable( (f.name, f.attname) if f.concrete else (f.name,) for f in opts.get_fields() @@ -301,7 +303,8 @@ class Query(BaseExpression): processing. Normally, this is self.model._meta, but it can be changed by subclasses. """ - return self.model._meta + if self.model: + return self.model._meta def clone(self): """ @@ -994,8 +997,10 @@ class Query(BaseExpression): if self.alias_map: alias = self.base_table self.ref_alias(alias) - else: + elif self.model: alias = self.join(self.base_table_class(self.get_meta().db_table, None)) + else: + alias = None return alias def count_active_tables(self): @@ -1619,6 +1624,8 @@ class Query(BaseExpression): field = None filtered_relation = None try: + if opts is None: + raise FieldDoesNotExist field = opts.get_field(name) except FieldDoesNotExist: if name in self.annotation_select: @@ -1673,7 +1680,7 @@ class Query(BaseExpression): # Check if we need any joins for concrete inheritance cases (the # field lives in parent, but we are currently in one of its # children) - if model is not opts.model: + if opts is not None and model is not opts.model: path_to_parent = opts.get_path_to_parent(model) if path_to_parent: path.extend(path_to_parent) |
