Django Admin Issue - 'NoneType'对象没有属性'user'

时间:2013-08-13 14:49:01

标签: django python-2.7 django-models django-admin django-users

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

'NoneType' object has no attribute 'user'

(下面的完整堆栈跟踪。)为什么响应对象突然变为None?如果我从字段集中删除字段,一切都很好。

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

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)

当我从字段集中删除'tag'时,一切都很好。可能是我正在制作一些简单的Django Admin错误,我以前没用过多少,但是我能找到的最好的谷歌搜索是三年前修复的一些错误,我敢肯定我已经运行了1.5 0.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'

1 个答案:

答案 0 :(得分:2)

您的吞咽请求。在这里你可以理解:

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)