Skip to content
Advertisement

(DRF) How to update a foreignkey field

I have two models, Account model & Thread model:

JavaScript
JavaScript

If I have already created a thread that is ForeignKey to the Account model, I am not able to change the username of the Account model, returning the error FOREIGN KEY constraint failed. I guess the existing Thread model require a username to point to. Is there way to create a custom update method in view.py to update the ForeignKey automatically?

Here is my view.py:

JavaScript

EDIT: serializer.py

JavaScript

Error:

JavaScript

Advertisement

Answer

delete to_field='username'

JavaScript

it is a reason of an error

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement