Python Django TransactionManagementError:select_for_update不能在事务外部使用

Python Django TransactionManagementError:select_for_update不能在事务外部使用,python,django,django-models,django-rest-framework,django-views,Python,Django,Django Models,Django Rest Framework,Django Views,我使用DJANGO_db_路由器使用多数据库环境。但我面临着这个错误 TransactionManagementError:select\u for\u更新不能在事务之外使用。 Django事务在Django库中使用,因此我不能使用using=db_name。读和写都只发生在master上,但即使在我得到这个错误之后 ----> 1 create_type(t()) ~/setup.py in create_type(t) 1791 object_ident

我使用DJANGO_db_路由器使用多数据库环境。但我面临着这个错误

TransactionManagementError:select\u for\u更新不能在事务之外使用。

Django事务在Django库中使用,因此我不能使用using=db_name。读和写都只发生在master上,但即使在我得到这个错误之后

----> 1 create_type(t())

~/setup.py in create_type(t)
   1791             object_identifier=oi,
   1792             tenant=t,
-> 1793             defaults=data
   1794         )
   1795 

~/backend/venv/lib/python3.6/site-packages/django/db/models/manager.py in manager_method(self, *args, **kwargs)
     80         def create_method(name, method):
     81             def manager_method(self, *args, **kwargs):
---> 82                 return getattr(self.get_queryset(), name)(*args, **kwargs)
     83             manager_method.__name__ = method.__name__
     84             manager_method.__doc__ = method.__doc__

~/backend/venv/lib/python3.6/site-packages/django/db/models/query.py in update_or_create(self, defaults, **kwargs)
    501         with transaction.atomic(using=self.db):
    502             try:
--> 503                 obj = self.select_for_update().get(**lookup)
    504             except self.model.DoesNotExist:
    505                 obj, created = self._create_object_from_params(lookup, params)

~/backend/venv/lib/python3.6/site-packages/django/db/models/query.py in get(self, *args, **kwargs)
    395         if self.query.can_filter() and not self.query.distinct_fields:
    396             clone = clone.order_by()
--> 397         num = len(clone)
    398         if num == 1:
    399             return clone._result_cache[0]

~/cftr/backend/venv/lib/python3.6/site-packages/django/db/models/query.py in __len__(self)
    252 
    253     def __len__(self):
--> 254         self._fetch_all()
    255         return len(self._result_cache)
    256 

~/backend/venv/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()

~/backend/venv/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)

~/backend/venv/lib/python3.6/site-packages/django/db/models/sql/compiler.py in execute_sql(self, result_type, chunked_fetch, chunk_size)
   1054         try:
   1055             sql, params = self.as_sql()
-> 1056             if not sql:
   1057                 raise EmptyResultSet
   1058         except EmptyResultSet:

~/backend/venv/lib/python3.6/site-packages/django/db/models/sql/compiler.py in as_sql(self, with_limits, with_col_aliases)
    487                     if self.connection.get_autocommit():
    488                         raise TransactionManagementError('select_for_update cannot be used outside of a transaction.')
--> 489 
    490                     if with_limits and not self.connection.features.supports_select_for_update_with_limit:
    491                         raise NotSupportedError(

TransactionManagementError: select_for_update cannot be used outside of a transaction.