Images in Django fixtures?
Asked Answered
D

2

24

I was looking at Django fixtures in the docs, there was no mention of preloading images into the database. I can't be the only who needed to do this. Is this possible? If so how? If not, are there any workarounds available?

Please and thank you.

Demigod answered 20/4, 2011 at 4:38 Comment(0)
J
22

Yes, it's possible. You have to put your image in your project MEDIA_ROOT. In fixtures it will look like

[{
    "pk": 1,
    "model": "pictures.picture",
    "fields": {
        "image": "/media/gebakken_kip_met_korianderrijst_1152_x_1728.png"

    }
}]
Jotham answered 20/4, 2011 at 6:13 Comment(2)
This is adding a link to the image, not adding the image it self, but could do what you are looking for.Losel
In my case this leads to runtime error: The joined path (/media/images/logo1.png) is located outside of the base path component (/home/myuser/myproject/myproject/src/media)Corrida
S
8

this has been asked while ago and already been answered, i am still going to put this out there:

after dumping the fixture replace all the paths with one to a suitable placeholder image, that you manually put in your media directory. you probably should choose a path your app does not use in production, like "/media/fixture/sample.jpg". that way you could put the file into your scm of choice too.

if you have "/media" in your .gitignore (which is a good idea), don't forget to add an exception.

as an alternative, google also turns up this: https://github.com/leetrout/django-fixturemedia

Storekeeper answered 23/7, 2013 at 18:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.