Monday, December 28, 2015

Re: settings.py variables in fixtures

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.



Il giorno lunedì 28 dicembre 2015 01:19:05 UTC+1, Russell Keith-Magee ha scritto:
Hi Salvatore,

On Mon, Dec 28, 2015 at 12:09 AM, Salvatore Scaramuzzino <salvatores...@gmail.com> wrote:
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)?
 
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 %-)


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fe7d27d4-da96-4c95-987e-997ea2f0395d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment