Использование get_or_create
в Django вызывает следующую ошибку:
Traceback (most recent call last):
File "C:\Users\sande\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\sande\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 364, in execute
output = self.handle(*args, **options)
File "C:\Users\sande\Documents\projects\sbbetting\football\management\commands\scrape.py", line 12, in handle
sofa.get_matches()
File "C:\Users\sande\Documents\projects\sbbetting\football\common\sofascore.py", line 21, in get_matches
country = Country.objects.get_or_create(id=country_id, name=country_name, defaults={"id": country_id, "name": country_name})[0]
File "C:\Users\sande\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\sande\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\query.py", line 538, in get_or_create
return self.get(**kwargs), False
File "C:\Users\sande\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\query.py", line 402, in get
num = len(clone)
File "C:\Users\sande\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\query.py", line 256, in __len__
self._fetch_all()
File "C:\Users\sande\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\query.py", line 1242, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "C:\Users\sande\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\query.py", line 55, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "C:\Users\sande\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\sql\compiler.py", line 1098, in execute_sql
cursor = self.connection.cursor()
File "C:\Users\sande\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py", line 256, in cursor
return self._cursor()
File "C:\Users\sande\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py", line 233, in _cursor
self.ensure_connection()
File "C:\Users\sande\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py", line 217, in ensure_connection
self.connect()
File "C:\Users\sande\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py", line 197, in connect
self.init_connection_state()
File "C:\Users\sande\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\mysql\base.py", line 231, in init_connection_state
if self.features.is_sql_auto_is_null_enabled:
File "C:\Users\sande\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\sande\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\mysql\features.py", line 81, in is_sql_auto_is_null_enabled
with self.connection.cursor() as cursor:
File "C:\Users\sande\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py", line 256, in cursor
return self._cursor()
File "C:\Users\sande\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py", line 235, in _cursor
return self._prepare_cursor(self.create_cursor(name))
File "C:\Users\sande\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py", line 225, in _prepare_cursor
self.validate_thread_sharing()
File "C:\Users\sande\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py", line 547, in validate_thread_sharing
% (self.alias, self._thread_ident, _thread.get_ident())
django.db.utils.DatabaseError: DatabaseWrapper objects created in a thread can only be used in that same thread. The object with alias 'default' was created in thread id 14100 and this is thread id 67761232.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File ".\manage.py", line 21, in <module>
main()
File ".\manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:\Users\sande\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\sande\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\sande\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 336, in run_from_argv
connections.close_all()
File "C:\Users\sande\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\utils.py", line 224, in close_all
connection.close()
File "C:\Users\sande\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py", line 279, in close
self.validate_thread_sharing()
File "C:\Users\sande\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py", line 547, in validate_thread_sharing
% (self.alias, self._thread_ident, _thread.get_ident())
django.db.utils.DatabaseError: DatabaseWrapper objects created in a thread can only be used in that same thread. The object with alias 'default' was created in thread id 14100 and this is thread id 67761232.
Я написал следующий код:
def get_matches(self):
tournaments = self.data["sportItem"]["tournaments"]
for tournament in tournaments:
country_name = tournament["category"]["name"]
country_id = tournament["category"]["id"]
country = Country.objects.get_or_create(id=country_id, name=country_name, defaults={"id": country_id, "name": country_name})[0]
Следующая модель соответствует объекту 'Country' :
class Country(models.Model):
id = models.IntegerField()
name = models.CharField(max_length=200)
Кто-нибудь знает, как исправить эту ошибку?