0

我正在尝试构建一个管理面板,用户可以在其中使用一些规则填充数据库;对于某些实体/模型,每个用户只能查看和编辑自己的数据。

为此,我以这种方式继承了 modelAdmin 类

#my models

class Product(models.Model):
    name = models.CharField(max_length=80)
    description = models.TextField()
    author = models.ForeignKey(User)

    def __unicode__(self):
        return self.name

class Variant(models.Model):
    size = models.DecimalField(max_digits=3, decimal_places=0)
    author = models.ForeignKey(User)
    super_product = models.ForeignKey(Product)

    def __unicode__(self):
        return "%s %s" % (self.size)

#in urls.py

class FilterProduct(admin.ModelAdmin):
    def queryset(self, request):
        qs = super(FilterProduct, self).queryset(request)
        if request.user.is_superuser:
            return qs
        return qs.filter(author=request.user)

    def save_model(self, request, obj, form, change):
        if not request.user.is_superuser:
            obj.author = request.user

        obj.save()

class FilterVariant(admin.ModelAdmin):
    def queryset(self, request):
        qs = super(FilterVariant, self).queryset(request)
        if request.user.is_superuser:
            return qs
        return qs.filter(author=request.user)

    def save_model(self, request, obj, form, change):
        if not request.user.is_superuser:
            obj.author = request.user

        obj.save()

    def get_form(self, request, obj, **kwargs):
        form = super(FilerVariant,self).get_form(self,request, obj,**kwargs)

        if not request.user.is_superuser:
            form.base_fields['super_product'].queryset = form.base_fields['super_product'].queryset.filter(author=request.user)

        return form

admin.site.register(Product,FilterProduct)
admin.site.register(Variant,FilterVariant)

当我尝试添加变体时,出现此错误

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/admin/prodotti/varianti/add/
Django Version: 1.2.3
Python Version: 2.6.6
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'prodotti',
 'django.contrib.admin']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/usr/lib/pymodules/python2.6/django/core/handlers/base.py" in get_response
  100.                     response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/pymodules/python2.6/django/contrib/admin/options.py" in wrapper
  265.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/lib/pymodules/python2.6/django/utils/decorators.py" in _wrapped_view
  76.                     response = view_func(request, *args, **kwargs)
File "/usr/lib/pymodules/python2.6/django/views/decorators/cache.py" in _wrapped_view_func
  69.         response = view_func(request, *args, **kwargs)
File "/usr/lib/pymodules/python2.6/django/contrib/admin/sites.py" in inner
  190.             return view(request, *args, **kwargs)
File "/usr/lib/pymodules/python2.6/django/utils/decorators.py" in _wrapper
  21.             return decorator(bound_func)(*args, **kwargs)
File "/usr/lib/pymodules/python2.6/django/utils/decorators.py" in _wrapped_view
  76.                     response = view_func(request, *args, **kwargs)
File "/usr/lib/pymodules/python2.6/django/utils/decorators.py" in bound_func
  17.                 return func(self, *args2, **kwargs2)
File "/usr/lib/pymodules/python2.6/django/db/transaction.py" in _commit_on_success
  299.                     res = func(*args, **kw)
File "/usr/lib/pymodules/python2.6/django/contrib/admin/options.py" in add_view
  799.         ModelForm = self.get_form(request)

Exception Type: TypeError at /admin/prodotti/varianti/add/
Exception Value: get_form() takes exactly 3 arguments (2 given)

也许是一个愚蠢的问题,错误在哪里?我是否为函数提供了所有需要的参数?

4

1 回答 1

1

这里有几个错误。

首先,该get_form方法的原始签名是def get_form(self, request, obj=None, **kwargs)- 也就是说,obj参数是可选的(这是有道理的,因为当您创建一个新项目时,没有现有对象)。然而,你已经用这个覆盖了它:def get_form(self, request, obj, **kwargs)- 即现在obj参数是必需的。

除非您可以完全控制如何调用您的方法 - 在这种情况下您没有,因为它是由管理员完成的 - 您应该确保您的方法至少可以接受与原始方法相同的参数.

您的第二个错误在下一行:

form = super(FilerVariant,self).get_form(self,request, obj,**kwargs)

在这里,您复制了self参数 - 您不能在方法调用中显式传递它,因为它已经作为第一个参数传递。

最后,你绝对应该考虑升级——Django 1.2 是在两年前发布的,从那以后发生了很多变化。

于 2012-05-17T09:47:29.060 回答