django foreign key question.

Asked 2 years ago, Updated 2 years ago, 40 views

You are trying to save the file through the django foreign key.

First, extract the file from url that came in through view.py

The process of saving a file to a foreign key.

#models.py

class UploadModel(models.Model):
    uploadedFiles = models.FileField(blank=True,)

    def __str__(self):
        return self.uploadedFiles.name


class UploadURLmodel(models.Model):
    uploadURL = models.TextField(blank=False)
    # Foreign key associated with UploadModel above
    fileFromURL = models.ForeignKey(UploadModel, on_delete=models.CASCADE, blank=True, null=True)

    def save(self, *args, **kwargs):
        # Get file from url view
        if self.uploadURL and not self.fileFromURL:
            file_url = self.uploadURL
            For_file = UploadModel.objects.create() # The above UploadModel 
                                                    # the calling part

            file_name = file_url.split('/')[-1]

            response = get(file_url) # The process of extracting a file from url
            binary_data = response.content
            temp_file = BytesIO()
            temp_file.write(binary_data)
            temp_file.seek(0)
            # I don't know how to put data into the foreign key, so the foreign key points to it 
            # I inserted the file into the UploadModel instance and replaced it with a foreign key.
            for_file.uploadedFiles = temp_file
            for_file.save()
            self.fileFromURL = for_file #Put instance into foreign key

            print("wow")
            print(type(self.fileFromURL))

            self.fileFromURL.save(
                file_name,
                File(temp_file)# Error occurs here. The error contents are below.
            )
        super(UploadURLmodel, self).save()
        return self.fileFromURL.name

    def __str__(self):
        return self.fileFromURL.name
File "/Users/minhyeokjang/Desktop/github/fileConverter/fileconverter/models.py", line 43, in save
    File(temp_file)
  File "/Users/minhyeokjang/venv/lib/python3.7/site-packages/django/db/models/base.py", line 746, in save
    force_update=force_update, update_fields=update_fields)
  File "/Users/minhyeokjang/venv/lib/python3.7/site-packages/django/db/models/base.py", line 784, in save_base
    force_update, using, update_fields,
  File "/Users/minhyeokjang/venv/lib/python3.7/site-packages/django/db/models/base.py", line 887, in _save_table
    results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
  File "/Users/minhyeokjang/venv/lib/python3.7/site-packages/django/db/models/base.py", line 926, in _do_insert
    using=using, raw=raw,
  File "/Users/minhyeokjang/venv/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/minhyeokjang/venv/lib/python3.7/site-packages/django/db/models/query.py", line 1204, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "/Users/minhyeokjang/venv/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1390, in execute_sql
    for sql, params in self.as_sql():
  File "/Users/minhyeokjang/venv/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1335, in as_sql
    for obj in self.query.objs
  File "/Users/minhyeokjang/venv/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1335, in <listcomp>
    for obj in self.query.objs
  File "/Users/minhyeokjang/venv/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1334, in <listcomp>
    [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
  File "/Users/minhyeokjang/venv/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1285, in pre_save_val
    return field.pre_save(obj, add=True)
  File "/Users/minhyeokjang/venv/lib/python3.7/site-packages/django/db/models/fields/files.py", line 286, in pre_save
    if file and not file._committed:
AttributeError: '_io.BytesIO' object has no attribute '_committed'

This is the error content.

How can I save a file from url to a foreign key?

django

2022-09-21 11:46

1 Answers

First of all, I don't think I can answer the question you gave me because I'm ignorant of Python, and there's one thing I want to ask you in reverse. Why do you want to save "files" to DB (even to be indexed)?

I've been asking questions. Maybe it's because I need to make a GIF generator. GIF GIF GIF GENERATOR is

If it's an application that provides a series of... [em] That's because you don't need a DB in the first place.

I think it would be good to think of an old vending machine that doesn't work with cards. The vending machine doesn't store any user information. I just check the money that came in, and if there's no problem, I just pull out the drinks I want. (This is how the "YouTube Downloaders" in the section work.))

At this point, "Uh..." If you think, "No, I need a DB..." it's probably the actual scenario of the application you're implementing.

Because it's going to be the same. But actually, even in this case, you don't have to save the file in the DB at all. Because look, this scenario is similar to the Strong Laundry scenario.

Did you notice? The laundry owner is not holding the laundry.The only thing the dry cleaner has through the reggae is a tag. One dry cleaner said, "Oh... This customer left a total of three items, one of which is a coat made of white fabric that has been processed by some kind of technique, and he doesn't remember. There's no way to push the laundry into the reggae.

In summary, if you have information that you need to store in a GIF generator scenario, that would be uniquely assignable to the original image. "With that information, you can get that video/gif," something like that laundry tag. It would be most convenient to use a local path that stores that image/gif. And in that general case, the foreign key will work just as well as you would expect.

And the GIF itself is the tag information that you need to find on a particular storage, not information to be stored in the DB. In the first place, storing the entire response.content result value in the DB is also a security bad idea. What if you get *; DROP TABLE; when you put SELECT on that value? (I'm not exaggerating.)

For the first time I'll be back again now. Why? to save (Even the values to be indexed,) in the "file" db Am I wrong or missing, if there is something that I would be grateful if you would give us.


2022-09-21 11:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.