如何在django模型中使用pre_init

时间:2012-10-23 15:32:42

标签: django model signals

我想* pre_init *我的模型的订单值作为具有最大订单值的其他对象的递增值。我写了这样的函数:

@receiver(pre_init, sender=Category)
def get_order(instance, **kwargs):    
    previous = Category.objects.all().order_by('-order')[0].order
    instance.order = previous + 1

但是当我运行管理面板时,我会收到这样的错误:

Environment:


Request Method: GET Request URL: http://127.0.0.1:8000/admin/catalog/category/add/

Django Version: 1.5 Python Version: 2.7.2 Installed Applications: ('django.contrib.auth',  'django.contrib.contenttypes',  'django.contrib.sessions',  'django.contrib.sites',  'django.contrib.messages',  'django.contrib.staticfiles',  'django.contrib.flatpages',  'django.contrib.admin',  'utils',  'menu',  'contact',  'mediamanager',  'mptt',  'catalog') Installed Middleware: ('django.contrib.sessions.middleware.SessionMiddleware',  'django.middleware.locale.LocaleMiddleware',  'django.middleware.common.CommonMiddleware',  'django.contrib.auth.middleware.AuthenticationMiddleware',  'django.contrib.messages.middleware.MessageMiddleware',  'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware')


Traceback: File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs) File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in wrapper
  367.                 return self.admin_site.admin_view(view)(*args, **kwargs) File "C:\Python27\lib\site-packages\django\utils\decorators.py" in
_wrapped_view
  91.                     response = view_func(request, *args, **kwargs) File "C:\Python27\lib\site-packages\django\views\decorators\cache.py" in
_wrapped_view_func
  89.         response = view_func(request, *args, **kwargs) File "C:\Python27\lib\site-packages\django\contrib\admin\sites.py" in inner
  202.             return view(request, *args, **kwargs) File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapper
  25.             return bound_func(*args, **kwargs) File "C:\Python27\lib\site-packages\django\utils\decorators.py" in
_wrapped_view
  91.                     response = view_func(request, *args, **kwargs) File "C:\Python27\lib\site-packages\django\utils\decorators.py" in bound_func
  21.                 return func(self, *args2, **kwargs2) File "C:\Python27\lib\site-packages\django\db\transaction.py" in inner
  208.                 return func(*args, **kwargs) File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in add_view
  971.             form = ModelForm(initial=initial) File "C:\Python27\lib\site-packages\django\forms\models.py" in __init__
  235.             self.instance = opts.model() File "C:\Python27\lib\site-packages\django\db\models\base.py" in __init__
  318.         signals.pre_init.send(sender=self.__class__, args=args, kwargs=kwargs) File "C:\Python27\lib\site-packages\django\dispatch\dispatcher.py" in send
  167.             response = receiver(signal=self, sender=sender, **named)

Exception Type: TypeError at /admin/catalog/category/add/ Exception Value: get_order() takes exactly 1 argument (0 given)

有人知道如何修复它吗?或者应该如何看待pre_init信号功能?

1 个答案:

答案 0 :(得分:1)

快速检查django docs表明pre_init没有采用实例参数。这是参数计数不匹配的来源。

从您的示例中,您可能需要一个post_init信号处理程序,它确实需要一个实例值。