如何讓django通過燈具上傳圖像文件

[英]How to get django to upload an image file via fixtures


I have a class in my models.py:

我的models.py中有一個類:

class Car(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4)
    create_date = models.DateTimeField('date added', auto_now_add=True)
    modify_date = models.DateTimeField('date modified', default=timezone.now)
    name = models.CharField(max_length=200)
    image_file = models.ImageField(
        upload_to=upload_to_company, null=True, blank=True)

and a JSON for fixtures like this:

和像這樣的燈具的JSON:

   [
      {
        "model": "polls.car",
        "fields": {
            "id":"09734145-7a15-4c84-8b7c-eaab4112cbaa",
            "name": "VW",
            "create_date": "2015-01-01T00:00:00+00:00"
        }
      }
    ]

which I load as a fixture. Now I want it to get it to load an image as well, however when I insert "image_file":"VW.jpg" (VW.jpg is in the same folder as the JSON). VW.jpg shows up in the admin's panle but it doesn't upload the actual picture.

我作為一個夾具加載。現在我想讓它加載一個圖像,但是當我插入“image_file”時:“VW.jpg”(VW.jpg與JSON在同一個文件夾中)。 VW.jpg顯示在管理員的panle中,但它不上傳實際圖片。

Anyone an idea or a hint for where to look?

任何人有想法或暗示在哪里看?

1 个解决方案

#1


1  

No idea why I didn't come earlier to this conclusion but it works if you just place the file with name specified in the fixtures file in the directory you want to have it. For example:

不知道為什么我沒有提前得出這個結論,但如果您只是將具有在fixtures文件中指定的名稱的文件放在您想要的目錄中,它就會起作用。例如:

 [
      {
        "model": "polls.car",
        "fields": {
            "id":"09734145-7a15-4c84-8b7c-eaab4112cbaa",
            "name": "VW",
            "create_date": "2015-01-01T00:00:00+00:00",
            "image_file":"VW.jpg"
        }
      }
    ]

and then put VW.jpg where your media directory is located, for example:

然后將VW.jpg放在媒體目錄所在的位置,例如:

media/VW.jpg

It doesn't get uploaded this way but it's in the database and accessible over the admins panel

它不會以這種方式上傳,但它位於數據庫中,可通過管理員面板訪問


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/03/23/34b4b2ad074d9c4b895d083e28b643e9.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com