2012-05-17 49 views
0

我試圖建立一個管理面板,用戶可以用一些規則填充數據庫;每個用戶都可以查看和編輯某些實體/模型,只有自己的數據。django 1.2 modelAdmin繼承類,錯誤「get_form()需要剛好3個參數(2給出)」

爲做到這一點,我繼承一個類的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) 

當我嘗試添加一個Variant我得到這個錯誤

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) 

也許是一個愚蠢的問題,錯誤在哪裏?我給這個函數所有需要的參數嗎?

回答

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是兩年前發佈的,自那以後發生了很多變化。

+0

非常感謝。無論如何,我知道新版本更好,但我無法更改安裝在服務器上的軟件:) – Infrid

相關問題