E
E
Egegey2020-05-07 13:59:22
Django
Egegey, 2020-05-07 13:59:22

Django. What is the correct way to save the generated file in a FileField?

Hey!

I am extremely inexperienced with Django and need help.
I tried to override the save() method so that a QR code is generated and then saved.
However, when I save the recording in the media folder, I have two files.
5eb3e75b5416c152823951.png
One file ("10.png") consists of the model's slug field and extension.
The file is created after executing this line:

self.qr.save(self.slug+'.png', BytesIO(qr), save=False)

The name of another file is generated by Django itself (after the creation of the first one) and saved in the qr field of the model.

How can I make sure that only one file is created (only "10.png") and that it is saved in the field?

Model.py
class Url(models.Model):
    slug = models.CharField(max_length=50, unique=True)
    qr = models.FileField(upload_to='', blank=True, null=True)

    def save(self, *args, **kwargs):
        qr = self.qr_generate(self.slug)
        self.qr.save(self.slug+'.png', BytesIO(qr), save=False)
        super(Url, self).save(*args, **kwargs)

    def __str__(self):
        return self.slug

    @staticmethod
    def qr_generate(slug):
        qr = qrcode.QRCode(
            version=None,
            error_correction=qrcode.constants.ERROR_CORRECT_L,
            box_size=10,
            border=4,
        )
        qr.add_data("somedata" + str(slug))
        qr.make(fit=True)

        img = qr.make_image(fill_color="black", back_color="white")
        qrByte = BytesIO()
        img.save(qrByte)
        return qrByte.getvalue()

Answer the question

In order to leave comments, you need to log in

7 answer(s)
V
Vadim, 2020-05-07
@Viji

What if we remove super(Url, self).save(*args, **kwargs) ?

J
JRazor, 2020-05-08
@JRazor

You can move the QR to a separate model and work inside it with a qr code. And in the URL already tie the object through OneToOne

S
sanchemba, 2020-06-12
@sanchemba

I had the same issue. The fact is that when you bind a file to a field on your own, you probably need to disable the file name generator in the FileField field and / or enable file overwriting. How to do this, and whether it is possible at all, I have not figured out yet. Obviously, when saving a model instance and, accordingly, a field (in your case qr ), the presence of a file is checked at the path specified in path (and you, if I understand correctly, do this by calling self.qr.save() ). If the specified path is "busy", the file is not overwritten, but a new filename is generated.
This is roughly how it works. If someone tells me how to change the behavior or override the save method of the FileField field, I will be very happy.
I did it rather clumsily:
I have a separate method responsible for compiling file names, which is passed in the upload_to field and it will determine the final file name.
In the overridden save() method of the model, I remember the path to the file, which I bind to the FileField field . After calling super(Url, self).save(*args, **kwargs) , I simply delete the temporary file (exactly the one that I attached myself, and the path to what really remained was determined in the method passed to upload_to when saving the model ).
I'm doing a service for myself, so I'm fine for now. But with a large stream of files, this can be quite expensive. So I keep looking for better ways...

B
batment, 2013-10-31
@batment

I was very surprised, but the $31 that I was asked for the Raspberry Pi A included express delivery by DHL from the UK, in 5 days. So it's worth giving them a chance :)
PS Yes, and as far as I understand, the OS is the most developed for it, I really worked on my own only with Arch Linux, and even then in console mode.

S
Sergey Lerg, 2013-11-01
@Lerg

The RPi has a lot of USB and Ethernet performance issues. But it is still the cheapest.
Most of all, it's a shame that you can't put the latest Ubuntu Arm on it, only special distributions.
There is a wonderful PandaBoard, but it is expensive.
Look at BeagleBoard and Cubieboard.

A
Andrey, 2013-11-01
@svistiboshka

www.terraelectronica.ru/catalog_info.php?ID=827&CODE=994493&ushko_c=2

D
Dmitry Batin, 2015-04-21
@demoss

I propose to look in the direction of ODROID.
I have already set my sights on them and am going to take 3 pieces of iron for development.
Top, micro and medium. (XU\W\U3)
www.hardkernel.com/main/main.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question