TL無屬性;博士Django管理 - 「ManyToManyField」對象有「到」
的Django拋出AttributeError
當我試圖渲染管理類使用ManyToMany
關係,通過自定義表單fields
或fieldsets
屬性,除了form
屬性:
AttributeError: 'ManyToManyField' object has no attribute 'through'
型號:
我有兩個模型綁定與ManyToMany
關係:
class Project(models.Model):
title = models.CharField(max_length=200, unique=True)
class Centre(models.Model):
projects = models.ManyToManyField(
Project, blank=True, related_name='centres',
)
表:
而且,我創建了一個自定義窗體爲Project
模型添加FilteredSelectMultiple
領域Centre
:
class ProjectAdminForm(forms.ModelForm):
centres = forms.ModelMultipleChoiceField(
Centre.objects.all(),
widget=admin.widgets.FilteredSelectMultiple('Centres', False),
required=False,
)
def __init__(self, *args, **kwargs):
super(ProjectAdminForm, self).__init__(*args, **kwargs)
if self.instance.pk:
self.initial['centres'] = self.instance.centres.values_list(
'pk', flat=True
)
def save(self, *args, **kwargs):
instance = super(ProjectAdminForm, self).save(*args, **kwargs)
if instance.pk:
instance.centres.clear()
instance.centres.add(*self.cleaned_data['centres'])
return instance
Admin:
的事情是,當我定義管理類這樣的:
@admin.register(Project, site=admin_site)
class ProjectAdmin(admin.ModelAdmin):
form = ProjectAdminForm
它工作正常,並呈現爲title
和centres
領域一個正確的形式。
問題
但是,如果我定義字段,例如:
@admin.register(Project, site=admin_site)
class ProjectAdmin(admin.ModelAdmin):
form = ProjectAdminForm
fields = ('title', 'centres',)
這將引發AttributeError
:
AttributeError: 'ManyToManyField' object has no attribute 'through'
我試過在Project
設置上models.ManyToManyField
through
屬性模型,定義中介ProjectCentresModel
,但我仍然無法理解Django內部發生了什麼,我應該調整以使其工作。
回溯:
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f88ac6e5bf8>
Traceback (most recent call last):
File "/media/kidbinary/Data/web/PYTHON/.virtualenvs/work/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/media/kidbinary/Data/web/PYTHON/.virtualenvs/work/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 113, in inner_run
autoreload.raise_last_exception()
File "/media/kidbinary/Data/web/PYTHON/.virtualenvs/work/lib/python3.5/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/media/kidbinary/Data/web/PYTHON/.virtualenvs/work/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/media/kidbinary/Data/web/PYTHON/.virtualenvs/work/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/media/kidbinary/Data/web/PYTHON/.virtualenvs/work/lib/python3.5/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/media/kidbinary/Data/web/PYTHON/.virtualenvs/work/lib/python3.5/site-packages/django/apps/registry.py", line 115, in populate
app_config.ready()
File "/media/kidbinary/Data/web/PYTHON/.virtualenvs/work/lib/python3.5/site-packages/django/contrib/admin/apps.py", line 23, in ready
self.module.autodiscover()
File "/media/kidbinary/Data/web/PYTHON/.virtualenvs/work/lib/python3.5/site-packages/django/contrib/admin/__init__.py", line 26, in autodiscover
autodiscover_modules('admin', register_to=site)
File "/media/kidbinary/Data/web/PYTHON/.virtualenvs/work/lib/python3.5/site-packages/django/utils/module_loading.py", line 50, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "/media/kidbinary/Data/web/PYTHON/.virtualenvs/work/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/media/kidbinary/Data/web/PYTHON/work_project/work/website/admin.py", line 165, in <module>
class ProjectAdmin(DefaultOrderingModelAdmin):
File "/media/kidbinary/Data/web/PYTHON/.virtualenvs/work/lib/python3.5/site-packages/django/contrib/admin/decorators.py", line 28, in _model_admin_wrapper
admin_site.register(models, admin_class=admin_class)
File "/media/kidbinary/Data/web/PYTHON/.virtualenvs/work/lib/python3.5/site-packages/django/contrib/admin/sites.py", line 110, in register
system_check_errors.extend(admin_obj.check())
File "/media/kidbinary/Data/web/PYTHON/.virtualenvs/work/lib/python3.5/site-packages/django/contrib/admin/options.py", line 117, in check
return self.checks_class().check(self, **kwargs)
File "/media/kidbinary/Data/web/PYTHON/.virtualenvs/work/lib/python3.5/site-packages/django/contrib/admin/checks.py", line 515, in check
errors = super(ModelAdminChecks, self).check(admin_obj)
File "/media/kidbinary/Data/web/PYTHON/.virtualenvs/work/lib/python3.5/site-packages/django/contrib/admin/checks.py", line 71, in check
errors.extend(self._check_fieldsets(admin_obj))
File "/media/kidbinary/Data/web/PYTHON/.virtualenvs/work/lib/python3.5/site-packages/django/contrib/admin/checks.py", line 155, in _check_fieldsets
for index, fieldset in enumerate(obj.fieldsets)
File "/media/kidbinary/Data/web/PYTHON/.virtualenvs/work/lib/python3.5/site-packages/django/contrib/admin/checks.py", line 155, in <listcomp>
for index, fieldset in enumerate(obj.fieldsets)
File "/media/kidbinary/Data/web/PYTHON/.virtualenvs/work/lib/python3.5/site-packages/django/contrib/admin/checks.py", line 190, in _check_fieldsets_item
for fieldset_fields in fieldset[1]['fields']
File "/media/kidbinary/Data/web/PYTHON/.virtualenvs/work/lib/python3.5/site-packages/django/contrib/admin/checks.py", line 190, in <listcomp>
for fieldset_fields in fieldset[1]['fields']
File "/media/kidbinary/Data/web/PYTHON/.virtualenvs/work/lib/python3.5/site-packages/django/contrib/admin/checks.py", line 204, in _check_field_spec
return self._check_field_spec_item(obj, model, fields, label)
File "/media/kidbinary/Data/web/PYTHON/.virtualenvs/work/lib/python3.5/site-packages/django/contrib/admin/checks.py", line 220, in _check_field_spec_item
if field.many_to_many and not field.remote_field.through._meta.auto_created:
AttributeError: 'ManyToManyField' object has no attribute 'through'
請將完整的錯誤追溯添加到您的問題! –
當您試圖通過管理員中的manytomanyfield顯示項目列表時,我理解了第一個問題。這是你的初衷嗎? –
是的,也就是說,但我想將它們顯示爲「FilteredSelectMultiple」,而不是堆疊或表格內聯(可以工作)。這個問題真的是爲什麼當我在admin類中明確定義字段時它會拋出錯誤。 – Nevertheless