[Answered ]-How can I keep 'username' after deleting user in django?

1👍

Instead of using models.ForeignKey to set author attribute in Comment model, try CharField, and add an assignment line in your comment function in views.py to let this Comment.author attribute equals to its author name every time an author create it.

Hope this helps 🙂

1👍

it was setting null because you defined the author as null=True and on_delete=models.SET_NULL, to keep the author id intact you can use on_delete=models.DO_NOTHING but as the doc says

you need to manually add the ON DELETE constraint into the database
field

but to really keep the username here you need to denormalize it and use a separate username field which you need to set on saving the comment.

Leave a comment