M
M
Maxim Zubenko2018-12-28 22:53:09
Django
Maxim Zubenko, 2018-12-28 22:53:09

How to add many records from a file at once in Django model?

There is a very simple model:

class Color(models.Model):
    title = models.CharField(max_length=30, verbose_name="Название (код) ЛКМ", unique=True)
    image = ThumbnailerImageField(default='img/thumb.png', resize_source=dict(quality=95, size=(512, 512), sharpen=True))
    priority = models.IntegerField(default=0, verbose_name="Приоритет сортировки")
    is_active = models.BooleanField(default=True)

There is a form class:
class ColorForm(forms.ModelForm):
    class Meta:
        model = Color
        fields = ['title', 'image', 'priority', 'is_active']
        labels = {
            'title': 'Название',
            'image': 'Изображение',
            'priority': 'Приоритет сортировки',
            'is_active': 'Активен (включен)',
        }
        help_texts = {
            'priority': 'используется для отображения порядка при выборе, чем больше число, тем выше в списке',
            'is_active': 'если выключен, то не показывается в каталоге',
        }

At first everything was fine, but today the client asks a question: We are switching to color codes of some catalog, and there are about 2500 thousand colors. Is it possible to fill everything through the machine?
They say they will prepare a *.csv or *.txt file with codes, but pictures are not needed in principle.
And now the question is how best to make it so that the manager can click on the button to select a file and it fills the model with the necessary values ​​\u200b\u200bwithout any "freezes" there. And at the same time, it was also possible (as it is now) to add color "one by one".
Sincerely

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey Krasnodemsky, 2018-12-29
@Prognosticator

At first everything was fine, but today the client asks a question

Here he smiled. Changing the software is "normal".
The wording of the problem is also fun.
And yes, it's a funny question.
I will abstract and answer regardless of the PL and the web framework (that is, I will not write the code for you - in eremotheria mode).
I think you will figure it out with file parsing anyway, write a worker (executed in a separate process, "against dependencies") launched by submitting the file upload form. In the worker, go through the file line by line, save the extracted data through a batch insert into the database used (today 2500, tomorrow suddenly millions.). And not the essence of txt, csv, xlsx, by the way, you can offer the customer both this and that together. Well, somehow notify the manager, because she will wait for the download to finish, she will worry. What if the uploaded file contains formatting that breaks the file parsing process? Need to catch the error, save the intelligible error text and show it to the manager, because she should know that the file needs to be fixed and try to upload it again? After all, we are humans, not animals.
Well, it would be necessary to write tests, do you want to sleep a deeper sleep knowing that this added feature continues to work when you change the code?
Do you need to make a record update mechanism? Then the uploaded file must have unique identifiers. You will somehow determine which "color" from the file corresponds to "color" in the database.
Or do you need to recreate the records? Depending on this, write logic.
Well, think about the UI yourself, where to add the link, where to display the form.

S
Sergey Gornostaev, 2018-12-29
@sergey-gornostaev

django-import-export

V
Vadim Shatalov, 2018-12-29
@netpastor

Customize the admin panel, add a view with the desired template, post the url to it in templates/admin/{model}_change_list.html
https://docs.djangoproject.com/en/2.1/ref/contrib/...

V
Vladimir, 2018-12-29
@vintello

if this is a one-time situation - directly upload to the
mysql database and postgres can do this out of the box

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question