B
B
bQ12019-08-27 09:55:06
Django
bQ1, 2019-08-27 09:55:06

How to bind one model to two others on one Django field?

Perhaps the question is somehow not correctly formulated, but the essence is this:
There is a Posts and Profile model, as planned, everyone should have comments (comments on the post and comments on the profile)
Comments have a very large and long structure: Comment, like comment, reply to comment , like the answer, etc. I would not want to copy the same thing to bind it to Profile. How to act in such a situation?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Timoshko, 2019-08-30
@bQ1

Foreignkey can be used
Briefly:
Used to bind one model to another. In this case, you can bind your Posts and Profile models to Comments.
An example of the code is in the documentation, to which I have already left a link for you. And here is a brief explanation of the arguments:
First argument: Name of the Model to which we want to bind
Second argument: on_delete=models.CASCADE(optional), used if, when deleting the main record, you need to delete the current model as well
. Next as usual.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question