Access Image From Media Folder in views

I want to create a barcode image(using python-barcode library) for every item of a database and save this image file to the database.
How to do this?

generateBarcode function

Are you looking to do this as a one-time “batch” function, or for each file when they’re uploaded?

If you’re looking to do them all-at-once as a batch, you can do this in a custom manage.py command.

If you’re looking to do this for each file when uploaded, see Uploaded files and upload handlers for working with uploaded files in your view.

Thanks for your reply.
Maybe I should’ve been more specific about my problem.

A form with id will be submitted through POST request. When the view function will catch, it will create an barcode-image (jpg or png) and save it into a folder (Not into database) .
From the view function I want to grab this barcode-image (What was created and saved to folder in the runtime) and want to save this image into imagefield of a database for everytime the form is submitted.

The users themselves will not upload any image for barcode, If it is then I can grab this image from request.FILES

You can think of my model class something like below:

class CardPerson(models.Model):
    card_id = models.BigIntegerField(primary_key=True)
    barcode_photo = models.ImageField(upload_to = "media/barcodes/" , default = "default.jpg")

An ImageField does not store the image in the database. The ImageField is a link to the location of the file in the file system.

So, your process should create the image, then use that information to build the ImageField. See FileField and FieldFile, particularly the save method.

thank you @KenWhitesell . it worked…

working view function

def create_card(request):
    if request.method == 'POST':
        id = request.POST['card_id']
        card_person_form =  CardPersonForm(request.POST, request.FILES)
       
        if card_person_form.is_valid():
            card = card_person_form.save(commit=False)
            barcode_path = generateBarcode("123456667")

            # print(barcode_path)

            barcode_file = open(barcode_path, "rb")
            card.barcode_photo.save("mybarcode.png", File(barcode_file))
            barcode_file.close()

    return render(request, 'cards/create_card.html')