settings.py variables in fixtures

32 views
Skip to first unread message

Salvatore Scaramuzzino

unread,
Dec 27, 2015, 12:02:42 PM12/27/15
to Django users
hi to everyone

i'm newbye in django, but i really appreciate this useful framework. I have a simple question but i don't know if is appropriate.

i have a fixture written in JSON and i use it to provide initial data to my webapp. i want to provide in this fixture absolute paths in order to store some images..... is there a way to use in JSON fixtures (or in another format) variables defined in settings.py (eg. base path of the app)?

thanks in advance

Russell Keith-Magee

unread,
Dec 27, 2015, 7:19:05 PM12/27/15
to Django Users
Hi Salvatore,

To make sure I’ve understood your use case: you want to have a JSON (or similar format) file that contains absolute paths to some images, and load these images into your database, relative to some setting in your settings.py file.

If that’s the case, you probably won’t be able to use Django’s default serializer framework - Django’s serialisers are designed to be little more than a dump (and restore) of literal database content. Transforming input along the way isn’t something they are designed to do. You *might* be able to do something by writing a custom field and manipulating the deserialisation code on the field, but honestly, it’s going to be more trouble than it’s worth.

What you are trying to do is still completely achievable, though - you just have to write your own management command. That command will take the name of the JSON file as an argument, parse it and iterate through the content, and process the absolute paths into whatever local storage path you want to use. 

I hope that makes sense; if it doesn’t (or I’ve misunderstood your use case) let me know and I’ll elaborate some more.

Yours,
Russ Magee %-)


Salvatore Scaramuzzino

unread,
Dec 28, 2015, 8:26:51 AM12/28/15
to Django users
Thank you Russel, 

you centered my problem, I asked here in case exists a solution for this that i doesn't know.

I will write my own management script that "adapt" the dump data contained in JSON to the particular "installation" of the web application.

thanks again for your support.
Reply all
Reply to author
Forward
0 new messages