1

使用 Django 1.5 编写博客引擎并使用 Django Admin。一切都很好,直到我向模型添加了 ManyToManyField,并将该字段添加到 ModelAdmin 的字段集中,然后我在尝试加载管理页面时开始收到这个神秘的错误:

'NoneType' object has no attribute 'user'

(完整的堆栈跟踪在下面进一步。)为什么响应对象会突然变为无?如果我从字段集中删除该字段,一切都会再次正常。

我的模型看起来有点像这样(删除了很​​多字段):

class Tag(models.Model):
    name = models.CharField(max_length=30)

class Post(models.Model):
    title = models.CharField(max_length=300)
    tags = models.ManyToManyField(Tag)
    author = models.CharField(max_length=100)

ModelAdmin 看起来有点像这样:

class PostAdmin(admin.ModelAdmin):
    # Order
    fieldsets = (
        ('Content', {
            'fields': ('title', 'tags')
        }),
    )

    def formfield_for_dbfield(self, db_field, request, **kwargs):
        # Pre-fill 'author' with logged in name
        if db_field.name == "author":
            kwargs['initial'] = "%s %s" % (request.user.first_name, request.user.last_name)
            return db_field.formfield(**kwargs)
        return super(PostAdmin, self).formfield_for_dbfield(db_field, **kwargs)

当我从字段集中删除'标签'时,一切都恢复了。可能是我犯了一些简单的 Django Admin 错误,我以前没怎么用过,但我能找到的最好的谷歌搜索是三年前修复的一些错误,我确定我正在运行 1.5 .1。

这是完整的堆栈跟踪:

Internal Server Error: /theadmin/blog/post/1/
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 115, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "C:\Python27\lib\site-packages\django\contrib\admin\options.py", line 372, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "C:\Python27\lib\site-packages\django\utils\decorators.py", line 91, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\Python27\lib\site-packages\django\views\decorators\cache.py", line 89, in _wrapped_view_
unc
    response = view_func(request, *args, **kwargs)
  File "C:\Python27\lib\site-packages\django\contrib\admin\sites.py", line 202, in inner
    return view(request, *args, **kwargs)
  File "C:\Python27\lib\site-packages\django\utils\decorators.py", line 25, in _wrapper
    return bound_func(*args, **kwargs)
  File "C:\Python27\lib\site-packages\django\utils\decorators.py", line 91, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\Python27\lib\site-packages\django\utils\decorators.py", line 21, in bound_func
    return func(self, *args2, **kwargs2)
  File "C:\Python27\lib\site-packages\django\db\transaction.py", line 223, in inner
    return func(*args, **kwargs)
  File "C:\Python27\lib\site-packages\django\contrib\admin\options.py", line 1081, in change_view
    ModelForm = self.get_form(request, obj)
  File "C:\Python27\lib\site-packages\django\contrib\admin\options.py", line 465, in get_form
    return modelform_factory(self.model, **defaults)
  File "C:\Python27\lib\site-packages\django\forms\models.py", line 424, in modelform_factory
    return type(form)(class_name, (form,), form_class_attrs)
  File "C:\Python27\lib\site-packages\django\forms\models.py", line 212, in __new__
    opts.exclude, opts.widgets, formfield_callback)
  File "C:\Python27\lib\site-packages\django\forms\models.py", line 170, in fields_for_model
    formfield = formfield_callback(f, **kwargs)
  File "E:\Dropbox\PassionateAbout\PassionateAboutJustice\blog\admin.py", line 35, in formfield_for
dbfield
    return super(PostAdmin, self).formfield_for_dbfield(db_field, **kwargs)
  File "C:\Python27\lib\site-packages\django\contrib\admin\options.py", line 125, in formfield_for_
bfield
    related_modeladmin.has_add_permission(request))
  File "C:\Python27\lib\site-packages\django\contrib\admin\options.py", line 284, in has_add_permis
ion
    return request.user.has_perm(opts.app_label + '.' + opts.get_add_permission())
AttributeError: 'NoneType' object has no attribute 'user'
4

1 回答 1

3

你吞下了这个请求。给你:

def formfield_for_dbfield(self, db_field, request, **kwargs):

但是你不传递它:

return super(PostAdmin, self).formfield_for_dbfield(db_field, **kwargs)

但是原始 - https://github.com/django/django/blob/stable/1.5.x/django/contrib/admin/options.py#L88 被定义为:

def formfield_for_dbfield(self, db_field, **kwargs):

因此,您应该在覆盖时保持相同的签名。请注意:原版会:

request = kwargs.pop("request", None)

如果您想访问请求,请不要这样做,因为“pop()”会将其从 kwargs 中删除。只需访问而不删除:

request = kwargs['request']

所以你的超级电话仍然通过请求。

它只在 M2M 字段中出现,因为它需要查询当前用户(相关模型)的权限,而 CharField 并不真正需要该请求,所以不介意它是 None。(如果未找到请求,上面的 pop() 调用将执行此操作)

最终结果:

def formfield_for_dbfield(self, db_field, **kwargs):
    # Pre-fill 'author' with logged in name
    if db_field.name == "author":
        request = kwargs['request']
        kwargs['initial'] = "%s %s" % (request.user.first_name, request.user.last_name)
        return db_field.formfield(**kwargs)
    return super(PostAdmin, self).formfield_for_dbfield(db_field, **kwargs)
于 2013-08-13T20:19:39.773 回答