Django Admin - ' ManyToManyField'对象没有属性'通过'

时间:2016-11-29 09:50:22

标签: python django python-3.x django-admin many-to-many

TL;博士

当我尝试使用AttributeErrorManyToMany属性通过自定义表单呈现带有fields关系的管理类时,Django会抛出fieldsetsform属性除外:

  

AttributeError:' ManyToManyField'对象没有属性'到'

型号:

我有两个与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.register(Project, site=admin_site)
class ProjectAdmin(admin.ModelAdmin):
    form = ProjectAdminForm

它工作正常,并为titlecentres字段呈现正确的表单。

问题

但是,如果我这样定义字段:

@admin.register(Project, site=admin_site)
class ProjectAdmin(admin.ModelAdmin):
    form = ProjectAdminForm
    fields = ('title', 'centres', )

这会引发AttributeError

  

AttributeError:' ManyToManyField'对象没有属性'到'

我已尝试在through模型models.ManyToManyField设置Project属性,定义中间人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'

1 个答案:

答案 0 :(得分:1)

class ProjectAdmin(admin.ModelAdmin):
    model = Project
    form = ProjectAdminForm
    fields = ('title', 'centres',)


class CentreAdmin(admin.ModelAdmin):
    model = Centre
    list_display = ['id', 'get_projects']

    def get_projects(self, obj):
        #lists all the projects under a center without having to use inlines
        return "\n".join([proj.title for proj in obj.projects.all()])

老实说,我不能说为什么会抛出这个错误,添加模型只是猜测工作。我在django中检查了错误的源文件,但无法真正理解那里发生了什么。