https://supabase.com/ logo
Join Discord
Powered by
# python
  • j

    J0

    12/29/2021, 3:05 PM
    Anytim. Feel free to ping me again if you run into any issues
  • k

    Kebson

    12/29/2021, 3:06 PM
    Hey @User, I read your answer about @User question, but how can we perform an upload with Django so the file will go directly to Supabase ? Like, in a simple script I can upload file to supabase, but with Django, I have something like this:
    Copy code
    python
    class KhassidaPost(models.Model):
        def upload_image_to_supabase(instance, filename):
            filename_base, filename_ext = os.path.splitext(filename)
            #Rename this in bucket name
            path_name = 'cover/%s-%s' % (
                now().strftime("%Y%m%d%H%M%S"),filename)
            to_upload = os.path.dirname(os.path.abspath("__file__")) + filename
            print(client.storage().StorageFileAPI('qacidas-pdf').upload(path=path_name, file=str(instance.file.path)))
            public_upload_path = storage_file.get_public_url(path=path_name)
            return public_upload_path  
    
          def image_to_supabase():
            pass
    
        user        = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
        title       = models.CharField(max_length=120, null=True, blank=True)
        category       = models.CharField(max_length=120, null=True, blank=True, default="All")
        file        = models.FileField(upload_to=upload_image_to_supabase,blank=False,null=False)
        coverImage  = models.ImageField(upload_to=upload_image_to_supabase, null=True, blank=True)
        timestamp   = models.DateTimeField(auto_now_add=True)
    I tried to link it with my bucket, but it doesn't work.
  • j

    J0

    12/29/2021, 3:22 PM
    Hey Kebson, Thanks for the question! There are indeed many edges which need to be smoothened out. For media storage with Django we have an issue here: https://github.com/jschneier/django-storages/issues/1099 and we're actively patching bugs on the main client so feel free to file any issues you have on the
    supabase-py
    repository Can I check if
    qacidas-pdf
    is the name of your bucket?
    StorageFileAPI( )
    should take in a bucket so just wanted to be sure. Also, do you happen to have the error message that's returned? Lmk!
  • k

    Kebson

    12/29/2021, 3:46 PM
    Yes, it's the name of the bucket. It says me:
    Copy code
    FileNotFoundError: [Errno 2] No such file or directory:
    It means the file I try to upload whereas the file exists in the disk
  • k

    Kebson

    12/29/2021, 5:40 PM
    Do you have a simple example of uploading in Django to supabase ? Because only with this line :
    Copy code
    client.storage().StorageFileAPI('qacidas-pdf').upload(path=path_name, file=str(instance.file.path))
    called somewhere, I can upload the file. But what I want is, the public path of the uploaded file to be stored in the media field.
  • j

    J0

    12/30/2021, 8:56 AM
    Hey Kebson, Nope I unfortunately don't have one atm but will record an example over the weekend. Which media field are you referring to exactly?
  • k

    Kebson

    12/30/2021, 8:57 AM
    Ok no matter. I'm trying while waiting...
  • k

    Kebson

    12/30/2021, 8:58 AM
    Copy code
    python
     user        = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
        title       = models.CharField(max_length=120, null=True, blank=True)
        category       = models.CharField(max_length=120, null=True, blank=True, default="All")
        file        = models.FileField(upload_to=upload_image_to,blank=False,null=False)
        coverImage  = models.ImageField( null=True, blank=True)
        timestamp   = models.DateTimeField(auto_now_add=True)
    The file field
  • j

    J0

    12/30/2021, 8:59 AM
    Ah sorry, I still don't fully get it. Are you referring to saving the FileField under the
    media
    directory? Via the
    FileField(upload_to=<storage>)
    option?
  • k

    Kebson

    12/30/2021, 8:59 AM
    You know when you have for example a model User, and a field photo in the model
  • k

    Kebson

    12/30/2021, 9:00 AM
    But you want the photo file to be stored to supabase.
  • k

    Kebson

    12/30/2021, 9:00 AM
    So in the model you can have something like this:
  • k

    Kebson

    12/30/2021, 9:00 AM
    Copy code
    python
    file        = models.FileField(upload_to=upload_image_to,blank=False,null=False)
  • k

    Kebson

    12/30/2021, 9:01 AM
    upload_image_to is a function which send the photo to supabase.
  • j

    J0

    12/30/2021, 9:03 AM
    Ah yup! That's a great question, I'm actually working on a package to do that right now with the end goal of integrating it into Django Storages: Here's the issue: https://github.com/jschneier/django-storages/issues/1099 Here's the WIP package(Do not use): https://github.com/J0/django-storage-supabase There's actually another person who emailed me about this as well. Hoping to get it out by the weekend but it's not ready for use atm. Sorry about that D:
  • k

    Kebson

    12/30/2021, 9:04 AM
    Yes, I saw the package yesterday. I'll contribute on it once I found a solution
  • j

    J0

    12/30/2021, 9:07 AM
    ok thanks! Ah right I think you might have emailed me ytd, alright sure lmk!
  • k

    Kebson

    12/30/2021, 9:09 AM
    Yes it's me. I'm going forward with the solution. Now I upload, but the url is not well formed.
    Copy code
    json
    {
            "url": "http://127.0.0.1:8000/api/postings/56/",
            "id": 56,
            "user": 2,
            "title": "MyTitle",
            "category": "All",
            "file": "http://127.0.0.1:8000/media/https%3A/custom_url.supabase.co/storage/v1/object/public/qacidas-pdf/cover/20211_zoz3nqC.pdf",
            "coverImage": "http://127.0.0.1:8000/media/https%3A/custom_url.supabase.co/storage/v1/object/public/qacidas-pdf/cover/20211_pIpEsbm.png",
            "timestamp": "2021-11-09T11:46:20.390642Z"
    }
    This line, has two url. the local and the supabase url. I'll come back in by the week-end
    Copy code
    "http://127.0.0.1:8000/media/https%3A/custom_url.supabase.co/storage/v1/object/public/qacidas-pdf/cover/20211_pIpEsbm.png"
  • k

    Kebson

    12/30/2021, 9:12 AM
    See you soon
  • j

    J0

    12/30/2021, 3:03 PM
    Cya! Just came out of meeting so just saw this but yeah will update you on the status of the lib
  • a

    Anoushk

    12/31/2021, 8:43 AM
    @User any luck today? Need to demo someone my app
  • j

    J0

    12/31/2021, 9:00 AM
    Apologies about that, imagine it can be frustrating worrying about dependency issues before a demo when you might have other factors to worry about. Unfortunately, I don’t think we can publish an official release till the rest of the team comes online. What I can do though is publish an unofficial version to our old pypi namespace(Supabase_py)
  • j

    J0

    12/31/2021, 9:01 AM
    I’m walking home now, will get to it once I get back in about 15 mins
  • a

    Anoushk

    12/31/2021, 9:12 AM
    Ohk that works thanks
  • j

    J0

    12/31/2021, 10:22 AM
    here you go: the wheel file here should have the match command: https://replit.com/join/hjcquaxvup-aceofspade It is not ready for production use though so please do wait for the official release
  • j

    J0

    12/31/2021, 10:22 AM
    I've removed the
    dataclasses
    module as well so you shouldn't get
    from typing... dataclass
    typing errors
  • j

    J0

    12/31/2021, 10:24 AM
    To use in your own repl do the following: 1. Download the
    supabase-0.1.0-py3-none-any.whl
    whl file 2. Upload into your repl 3. Run
    pip3 uninstall supabase
    and then
    pip3 install supabase-0.1.0-py3-none-any.whl
    Lmk if you still face any issues!
  • a

    Anoushk

    12/31/2021, 10:46 AM
    ohk will let you know
  • a

    Anoushk

    12/31/2021, 10:51 AM
    Copy code
    ERROR: Could not install packages due to an OSError: [Errno 22] Invalid argument
  • a

    Anoushk

    12/31/2021, 11:22 AM
    @User im getting this error on repl and on railway too
1...101112...19Latest