Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3233878
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:19:39+00:00 2026-05-17T17:19:39+00:00

I have a requeriment to store images in the database using django, and for

  • 0

I have a requeriment to store images in the database using django, and for that I created a custom field :

from django.db import models

class BlobField(models.Field):

    __metaclass__ = models.SubfieldBase

    def db_type(self, connection):
        #TODO handle other db engines
        backend = connection.settings_dict['ENGINE']
        if backend == 'django.db.backends.postgresql':
            return 'bytea'
        elif backend == 'django.db.backends.sqlite3':
            return 'blob'
        else:
            raise Exception('unsuported db')

    def to_python(self, value):
        #TODO
        return value

    def get_db_prep_value(self, value, connection, prepared=False):
        #TODO              
        return value

I have already implemented a custom storage system to handle the storage/retrieval of images using a custom Model(that contains the above BlobField). The ‘value’ parameter in the ‘get_db_prep_value’ method is a ‘StringIO’ object that contains the image binary data. The catch is that I don’t know what to return in the ‘get_db_prep_value’ method since the ‘StringIO’ object will surely contain non-printable characters.

I have some questions about the problem:

  • What should I return in the ‘get_db_prep_value’ method?
  • If the expected value is an ASCII string, can I represent the blob(hexadecimal escapes) in a database independent way?
  • If not, are there built in libraries that handle this kind of conversion for me?
  • What can I expect to receive as input for the ‘to_python’ method, and how can I convert it to a StringIO object?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:19:39+00:00Added an answer on May 17, 2026 at 5:19 pm

    There is no constraint requiring get_db_prep_value to return “printable” characters, or ASCII ones, or otherwise-constrained sets of characters: return any byte string that catches your fancy. You’ll get a string in to_python and can make a file-like StringIO instance reading its data with the_instance = StringIO.StringIO(value) (of course you’ll need to import StringIO at the top of your module).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a requirement in project to store all the revisions(Change History) for the
We have a requirement to increase the functionality of a grid we are using
I have a requirement to be be able to embed scanned tiff images into
I have a requirement in my application that I think can be met by
In my application I have the requirement to store, for the period the user
I have a graph where user can draw the graph using touch gesture, i
I have a requirement to make a large amount of code MISRA compliant. First
I have a requirement to be able to provide a flex component in English
We have the requirement to take a form submission and save some data, then
I have a requirement to implement an Unsaved Changes prompt in an ASP .Net

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.