alex/django-fixture-generator

Outputting to a file called initial_data.json causes error

audreyfeldroy opened this issue · 5 comments

Outputting the fixture to the screen works fine for me. I can also output it to a file called initial_data.txt without any problem.

but when I try to output to a file called initial_data.json, I get an error:
(ea-iw)scaredofrabbits@ubuntu:~/code/ea-iw/project$ python manage.py generate_fixture products.test_products > initial_data.json
/home/scaredofrabbits/.virtualenvs/ea-iw/lib/python2.6/site-packages/django/db/models/fields/subclassing.py:80: DeprecationWarning: A Field class whose get_db_prep_value method hasn't been updated to take connection and prepared arguments.
new_class = super(SubfieldBase, cls).new(cls, name, bases, attrs)
/home/scaredofrabbits/.virtualenvs/ea-iw/lib/python2.6/site-packages/django/db/utils.py:27: DeprecationWarning: Short names for DATABASE_ENGINE are deprecated; prepend with 'django.db.backends.'
DeprecationWarning
Problem installing fixture 'initial_data.json': Traceback (most recent call last):
File "/home/scaredofrabbits/.virtualenvs/ea-iw/lib/python2.6/site-packages/django/core/management/commands/loaddata.py", line 169, in handle
for obj in objects:
File "/home/scaredofrabbits/.virtualenvs/ea-iw/lib/python2.6/site-packages/django/core/serializers/json.py", line 35, in Deserializer
for obj in PythonDeserializer(simplejson.load(stream), *_options):
File "/usr/lib/pymodules/python2.6/simplejson/init.py", line 267, in load
parse_constant=parse_constant, *_kw)
File "/usr/lib/pymodules/python2.6/simplejson/init.py", line 307, in loads
return _default_decoder.decode(s)
File "/usr/lib/pymodules/python2.6/simplejson/decoder.py", line 335, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/pymodules/python2.6/simplejson/decoder.py", line 353, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

Hi @audreyr

I'm very familiar with this application. I'm reading your traceback and I don't see django-fixture-generator getting called. It looks like loaddata command was called.

Are you 100% sure you run the right command, the right way? I usually output my output in a .json file without issues.

Cheers,
Miguel

Hi @maraujop

I've renamed the issue; I believe calling the output file something like whatever_you_want.json doesn't cause trouble, but calling it initial_data.json causes trouble.

It looks like something that's hard to fix without a ton of effort, and the documented workaround is not bad, so I'm closing it out. Here's the relevant docs change:
311a5f1

Hi @audreyr

I just renamed my file from initial_fixture.json to initial_data.json and it works flawlessly. Are you sure you are running this in a script that does a loaddata after generating the fixture?

@maraujop Let me see, hmm...

I have an app called "products". I got the error by running this:
python manage.py generate_fixture products.test_products > initial_data.json

or this:
python manage.py generate_fixture products.test_products > products/fixtures/initial_data.json

I didn't get the error while running this:
python manage.py generate_fixture products.test_products > initial_data.txt

Do you know that Django automatically does a loaddata of initial_data fixtures? Where is your initial_data.json file located - is it in the place where it would get loaded automatically as per the following?
https://docs.djangoproject.com/en/dev/howto/initial-data/#automatically-loading-initial-data-fixtures

Hi Audrey,

Well, I didn't know initial_data fixtures got loaded automatically. But I'm running a script that generates initial_data.json and then calls loaddata passing the file as an argument, without issues. Everything works smooth.

So I'm amazed you are having trouble with this, it's nuts.