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'
3

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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

For further reference see Official Documentation | Model Field Reference

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.