M
M
Michael2017-05-11 20:58:28
Django
Michael, 2017-05-11 20:58:28

How to rewrite ForeignKey relationships in django?

Hello, the bottom line is this. There are users, they create a database of their customers. Those. each client is bound via ForeignKey to user.
When a user is deleted, all associated clients will also be deleted. If deleted through the django admin, it warns that all connections and clients will be removed.
How to make it possible to choose which user to transfer all related clients to?
Those. so that in the admin panel, when deleting, you could choose which user to write connections to.
Help with advice or where to dig, what to read.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artyom Innokentiev, 2017-05-12
@artinnok

javedimka wrote everything correctly - you need to use SET(callback) . At the same time, you need to implement the logic that you want inside the callback .
Example actions:
1. Rewrite the part of the template responsible for deleting the object - insert the field with the pk of the user to which you want to transfer clients. Also, you may have to write something in JS.
2. Implement a `callback` method that returns a user object
3. Write on_delete=SET(callback)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question