我需要有关
解释的帮助为什么我们在同步适配器服务类
中使用单例对象
C:\Users\Will Treston\gd\LesRev\lesssonreview>python manage.py runserver
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x03363540>
Traceback (most recent call last):
File "C:\Users\Will Treston\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "C:\Users\Will Treston\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\commands\runserver.py", line 113, in inner_run
autoreload.raise_last_exception()
File "C:\Users\Will Treston\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "C:\Users\Will Treston\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\six.py", line 685, in reraise
raise value.with_traceback(tb)
File "C:\Users\Will Treston\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "C:\Users\Will Treston\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\Will Treston\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\apps\registry.py", line 115, in populate
app_config.ready()
File "C:\Users\Will Treston\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\admin\apps.py", line 23, in ready
self.module.autodiscover()
File "C:\Users\Will Treston\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\admin\__init__.py", line 26, in autodiscover
autodiscover_modules('admin', register_to=site)
File "C:\Users\Will Treston\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\module_loading.py", line 50, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "C:\Users\Will Treston\AppData\Local\Programs\Python\Python36-32\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "C:\Users\Will Treston\gd\LesRev\lesssonreview\review\admin.py", line 5, in <module>
class ReviewAdmin(admin.ModelAdmin):
File "C:\Users\Will Treston\gd\LesRev\lesssonreview\review\admin.py", line 8, in ReviewAdmin
list_display = ["title", Review.calculate_average()]
File "C:\Users\Will Treston\gd\LesRev\lesssonreview\review\models.py", line 46, in calculate_average
average += reply.replies.rating
AttributeError: 'ManyRelatedManager' object has no attribute 'rating'
答案 0 :(得分:1)
因为构建框架只能使用一个SyncAdapter:
同步适配器框架旨在使用作为单例实例的同步适配器组件。
您可以让多个组件触发SyncAdapter来发送一些数据。但您只想创建一个SyncAdapter来管理所有呼叫。因此,您使用单例模式创建SynAdapter。