K
K
krasdev2019-12-11 13:53:35
Django
krasdev, 2019-12-11 13:53:35

How to correctly add a User to an existing model?

How to correctly add a connection with the User to the existing model, so that the default author is already affixed to the previously created records, and the current one who creates the material is indicated on the new ones. I create
a field in the model - where I specify the default id of the user - but he uses it on new ones author = models.ForeignKey
(User, on_delete=models.CASCADE, default=2)
if you write without default
author = models.ForeignKey(User, on_delete=models.CASCADE)
then the error will be
NOT NULL constraint failed: project.author_id Tell me
how right to do?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vadim Shatalov, 2019-12-11
@krasdev

Or manually edit the migration
Or, when creating new authors, override in the code

T
tema_sun, 2019-12-11
@tema_sun

but he also uses it on new ones

So at the time of saving, you do not specify which User should be used.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question