You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用django-notifications时如何修复'on_delete'位置参数缺失错误?

Fixing "missing positional argument: 'on_delete'" in django-notifications

Looks like you're hitting a compatibility snag between your Django version and the older django-notifications package. Let's break down why this happens and how to fix it quickly:

Why the Error Occurs

Starting with Django 2.0, the ForeignKey field requires an explicit on_delete parameter to define what happens when the related object is deleted (e.g., cascade delete, block deletion, etc.). The version of django-notifications you're using was built for older Django versions and doesn't include this mandatory parameter in its model code—hence the TypeError pointing to line 170 of notifications/models.py.

The original django-notifications package is no longer actively maintained. Instead, use the community-supported fork django-notifications-redux, which supports Django 2.0+ and includes the required on_delete parameters out of the box:

  1. Uninstall the outdated package:
    pip uninstall django-notifications
    
  2. Install the updated fork:
    pip install django-notifications-redux
    
  3. Double-check your INSTALLED_APPS in settings.py still includes 'notifications' (the app name stays the same for the fork):
    INSTALLED_APPS = [
        # ... your other apps
        'notifications',
    ]
    
  4. Run migrations to sync the database schema with the updated model:
    python manage.py migrate
    

If you absolutely need to stick with the original django-notifications package, you can manually add the on_delete parameter to the problematic line. Note that this change will be overwritten if you update the package later:

  1. Open the file referenced in the error: C:\Python36\lib\site-packages\notifications\models.py
  2. Locate line 170, which looks like this:
    recipient = models.ForeignKey(settings.AUTH_USER_MODEL, blank=False, related_name='notifications')
    
  3. Add the on_delete=models.CASCADE parameter (you can pick other options like PROTECT or SET_NULL based on your needs):
    recipient = models.ForeignKey(settings.AUTH_USER_MODEL, blank=False, related_name='notifications', on_delete=models.CASCADE)
    
  4. Save the file and restart your Django server.

Quick Notes on on_delete Options

  • models.CASCADE: Delete the notification when the related user is deleted (most common use case)
  • models.PROTECT: Block deleting the user if there are linked notifications
  • models.SET_NULL: Set the recipient field to NULL (requires adding null=True to the field definition)

内容的提问来源于stack exchange,提问作者paula moreno gutierrez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:55:45