A
A
adrenalinruslan2021-07-13 16:50:10
Django
adrenalinruslan, 2021-07-13 16:50:10

Save model object to database?

How can the model be saved to the database? What is the point, there are 2 models, User and Status. The User model has a status field that is bound to a Status using a ForeignKey. When creating a user, I want to select the Status I need and, if necessary, change some fields in the Status. How to implement it? Is it easier to create a new Status and select it, or store the edited Status fields in JSON format?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Nesterov, 2021-07-13
@AlexNest

As an option - store the statuses in the database and add a boolean field to the form. And then - work according to the following algorithm: the user is saved -> If the field is selected - there is a redirect to the view, which will take the user id and the selected status. -> Based on the existing one, the view creates a new status and saves it. -> Updates the user record.
An alternative option is to add the update form to the registration page and work with it using js.
(By default - hidden -> when a boolean field is selected, show it -> substitute the base value data / use js`a to load from the database based on the option selected in the select and further down the list)
(there are fewer queries to the database, which is certainly good, but much more difficult to implement)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question