1

Please help me with this:

File "C:\Python36\lib\site-packages\notifications\models.py", line 170, in Notification
recipient = models.ForeignKey(settings.AUTH_USER_MODEL, blank=False, related_name='notifications')
TypeError: __init__() missing 1 required positional argument: 'on_delete'
Pieter Vandenheede
  • 3,128
  • 18
  • 34
  • is `Notification` class a django models? – Lemayzeur Apr 23 '18 at 05:26
  • 2
    Possible duplicate of [Getting TypeError: \_\_init\_\_() missing 1 required positional argument: 'on\_delete' when trying to add parent table after child table with entries](https://stackoverflow.com/questions/44026548/getting-typeerror-init-missing-1-required-positional-argument-on-delete) – cezar Apr 23 '18 at 08:13
  • check if this monkey-patch is what you need: https://stackoverflow.com/questions/50776076/django-1-to-django-2-on-delete-error/63111357#63111357 – Zaki Ahmed Jul 27 '20 at 09:10

2 Answers2

1

Use "on_delete=models.CASCADE" after the related_name='notifications'

For further reference see Official Documentation | Model Field Reference

  • 1
    `on_delete` is required, but you shouldn't assume that `models.CASCADE` is what the OP needs. Without an insight into the model is just a wild guess. Eventually `models.PROTECT` might be needed, or something else. – cezar Apr 24 '18 at 07:40
  • I agree. That's why I placed link to official Doc's. I'll take care about the insights in future references. Thanks for correcting. – Kartik Dhiman Apr 25 '18 at 12:14
1

Your Notification model has a ForeignKey relation to your user model. So a single user model instance can have multiple Notification instances associated with it. What on_delete means (what django is asking of you) is that if you delete an instance of your user model what should django do with all the associated Notification instances?

From django 2.x this argument became required.

Please read up on this to see all the options. But a quick rundown.

If you want all associated Notification instances to be deleted when you delete an instance of user model, set on_delete=models.CASCADE.

recipient = models.ForeignKey(settings.AUTH_USER_MODEL, blank=False, related_name='notifications', on_delete=models.CASCADE)

If you want the notification to remain untouched when you delete an instance of a user model, use on_delete=models.SET_NULL. But in this case you will have to set null=True on the recipient field. The notification will remain but it will not belong to any user.

recipient = models.ForeignKey(settings.AUTH_USER_MODEL, blank=False,  related_name='notifications', on_delete=models.SET_NULL, null=True)
chidimo
  • 1,826
  • 2
  • 20
  • 33