M
M
Maxim Zubenko2019-08-14 15:19:43
Django
Maxim Zubenko, 2019-08-14 15:19:43

How can I make a mechanism in Django whereby through the combined forms it is possible to change the values ​​​​of several records at once?

The point is. There is a settings model:

class Setting(models.Model):
    title = models.CharField(max_length=100, unique=True)
    slug = models.CharField(max_length=30, unique=True, blank=False)
    value = models.CharField(max_length=150, blank=False)
    
    def __str__(self):
        return self.title

There are 50 different settings there, and the values ​​​​are used here and there in the code. The settings are different. Mailbox authorizations (used for notifications), the same for keys for SMS, telegram bot. There are also bank details, signatures for forms and all sorts of other things.
The design is taken from a third-party project, where for work (CRUD) for this model, their own templates were used using REACT (and I'm still in this oak oak).
In general, I used only the principle "take from the database and use it as needed", and if I needed to change it, I changed it directly in the database, well, in extreme cases, through the Django admin panel.
Handed over the project to the client. It works, and ... in fact, I forgot and calmed down.
During this time, the management has changed there, those who served the sites. And in his head "piss hit" to make a special template for changing the settings. It is not the classic version, however.
By classic, I mean: List output, where there is a button opposite each item, by clicking on which you can edit one of the list values ​​on a separate page. Well, somewhere in the same place above the list there will be an "add" button, and also opposite each item there will be a "delete" button

So it doesn't want to. And wants, that at once the list with all fields was issued on the page.
1. Setting_name 1 | input_field_slug1 | Field_for_input_value1
2. Setting_name 2 | input_field_slug2 | Field_for_input_value2
3. Setting_name 3 | input_field_slug3 | Field_for_input_value3
...
57. Setting_name 57 | input_field_slug57 | Value_field_for_input57
and at the end there is only one "Save" button.
Let's argue, you will change these settings once in your life and forget. But it is stubborn, does not understand the arguments, but the brain endures.
Actually a question: How such to implement? Is it possible to do this without using Ajax/Js in the background (because it's a pain for me). Namely, so that he changes at least all the values ​​​​in these fields on one page and by clicking on the "save" button, everything is updated and changed there.
Where can I read about this? How do you do this in Django?
Sincerely.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Tikhonov, 2019-08-14
@tumbler

Django formsets

A
Anton Kuzmichev, 2019-08-14
@Assargin

If it's a good idea to implement this in a regular Django admin, then ModelAdmin.list_editable might be the solution . This attribute has been in jung for a long time. And once I even used it.
There is 1 pitfall, at least 3 years ago it definitely was: if more than 1 person changed data in the admin panel of the same model, then the data could be unpredictably lost and / or changed.
After digging into the sources then, I found out that this is due to the fact that when saving the edited data, djanga relies on the relative position of the object (!) in the queriset (!!) at the current moment !!!
That is, popularly speaking: do not "save the Super Item value in the title field of the instance id=345 of the MyModel model", but "save the Super Item value in the title field of the instance, which is in the current querise MyModel.objects.filter(category="Items ") on page #2 is in the 14th position".
And at first it may seem logical, they say, the classic case - the same data is changed by 2 or more people, of course you can get overwritten, but if only! Even just by adding a new object of this model, and not even from the admin panel, you can easily violate this relative "addressing".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question