Django QuerySet差异方法不起作用

时间:2018-08-03 17:42:18

标签: python django mariadb django-queryset

我试图获取两个QuerySet之间的区别,对我来说,重要的是,这个区别的答案应该是QuerySet。因此,自然的解决方案是使用Django queryset的差分方法。但是,当我尝试这样做时,我收到以下错误:

NotSupportedError: difference is not supported on this database backend.

这是我想做的事情:

In [4]: type(small_qs)
Out[4]: django.db.models.query.QuerySet

In [5]: type(bigger_qs)
Out[5]: django.db.models.query.QuerySet

In [6]: bigger_qs.difference(small_qs)

重要提示:两个查询集来自同一模型。

其他有用的信息:

  • 使用docker(数据库和Django)
  • 数据库后端是MariaDB(MySQL)
  • Django版本是2.0.6
  • MariaDB版本为10.3.8

这是完整的输出:

Out[6]: ---------------------------------------------------------------- 
NotSupportedError                         Traceback (most recent call 
last)
/usr/local/lib/python3.6/site-packages/IPython/core/formatters.py in 
__call__(self, obj)
700                 type_pprinters=self.type_printers,
701                 deferred_pprinters=self.deferred_printers)
702             printer.pretty(obj)
703             printer.flush()
704             return stream.getvalue()

/usr/local/lib/python3.6/site-packages/IPython/lib/pretty.py in 
pretty(self, obj)
398                         if cls is not object \
399                                 and 
callable(cls.__dict__.get('__repr__')):
400                             return _repr_pprint(obj, self, cycle)
401
402             return _default_pprint(obj, self, cycle)

/usr/local/lib/python3.6/site-packages/IPython/lib/pretty.py in 
_repr_pprint(obj, p, cycle)
693     """A pprint that just redirects to the normal repr function."""
694     # Find newlines and replace them with p.break_()
695     output = repr(obj)
696     for idx,output_line in enumerate(output.splitlines()):
697         if idx:

/usr/local/lib/python3.6/site-packages/django/db/models/query.py in         
__repr__(self)
246
247     def __repr__(self):
248         data = list(self[:REPR_OUTPUT_SIZE + 1])
249         if len(data) > REPR_OUTPUT_SIZE:
250             data[-1] = "...(remaining elements truncated)..."

/usr/local/lib/python3.6/site-packages/django/db/models/query.py in 
__iter__(self)
270                - Responsible for turning the rows into model 
objects.
271         """
272         self._fetch_all()
273         return iter(self._result_cache)
274

/usr/local/lib/python3.6/site-packages/django/db/models/query.py in 
_fetch_all(self)
1177     def _fetch_all(self):
1178         if self._result_cache is None:
1179             self._result_cache = list(self._iterable_class(self))
1180         if self._prefetch_related_lookups and not 
self._prefetch_done:
1181             self._prefetch_related_objects()

/usr/local/lib/python3.6/site-packages/django/db/models/query.py in 
__iter__(self)
51         # Execute the query. This will also fill compiler.select, 
klass_info,
52         # and annotations.
53         results = 
compiler.execute_sql(chunked_fetch=self.chunked_fetch, 
chunk_size=self.chunk_size)
54         select, klass_info, annotation_col_map = (compiler.select, 
compiler.klass_info,
55                                                   
compiler.annotation_col_map)

/usr/local/lib/python3.6/site-packages/django/db/models/sql/compiler.py 
in execute_sql(self, result_type, chunked_fetch, chunk_size)
1053             result_type = NO_RESULTS
1054         try:
1055             sql, params = self.as_sql()
1056             if not sql:
1057                 raise EmptyResultSet

/usr/local/lib/python3.6/site-packages/django/db/models/sql/compiler.py 
in as_sql(self, with_limits, with_col_aliases)
452             if combinator:
453                 if not getattr(features, 
'supports_select_{}'.format(combinator)):
454                     raise NotSupportedError('{} is not supported 
on this database backend.'.format(combinator))
455                 result, params = self.get_combinator_sql(combinator, 
self.query.combinator_all)
456             else:

NotSupportedError: difference is not supported on this database backend.

请,如果需要更多数据,请告诉我。

2 个答案:

答案 0 :(得分:1)

根据this ticket,django并未正式支持MariaDB。大多数功能显然都存在,但是确定版本或区分MySQL和MariaDB似乎有些困难。我猜测您看到的错误与此有关。

也就是说,根据您要执行的操作,difference可能实际上并不是解决问题的“自然方法”。如果(我怀疑)您的两个查询集实际上在同一模型上,则执行所需操作的最简单方法是使用exclude。遵循以下原则:

bigger_qs.exclude(id__in=smaller_qs)

这不会产生相同的SQL(difference使用EXCEPT,而exclude使用WHERE NOT),则应该产生相同的SQL集。结果(可能顺序不同)。

答案 1 :(得分:0)

此代码可能会帮助您

check = Qs1.objects.all()
prg=[]
[prg.append(x.ref) for x in check]
difference = (Qs2.objects.exclude(ref__in=prg).values())