Create table and load data

147 views
Skip to first unread message

Larry Martell

unread,
Oct 4, 2012, 12:26:55 PM10/4/12
to django...@googlegroups.com
Is there some way to use manage.py to create a table and load data
into it (where the data is contained in the models.py)?

I have a client that wants to have some data contained in the code (so
it's part of the code repository), and when they do the initial
syncdb, have that data loaded into one of the tables. Is anything like
this supported in django?

Jonathan Baker

unread,
Oct 4, 2012, 12:28:41 PM10/4/12
to django...@googlegroups.com
initial_data sounds like what you're looking for: https://docs.djangoproject.com/en/dev/howto/initial-data/


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.




--
Jonathan D. Baker
Developer
http://jonathandbaker.com

Larry Martell

unread,
Oct 4, 2012, 12:31:22 PM10/4/12
to django...@googlegroups.com
Thanks! I knew there had to be a way!

Jonathan Baker

unread,
Oct 4, 2012, 12:36:03 PM10/4/12
to django...@googlegroups.com
You're welcome. Personally, I've found it's easiest to get the JSON formatted correctly by creating the model, syncing the DB, inserting some dummy records, running 'dumpdata' against the table, and then copying the printed JSON into initial_data.json so that next time you build your data is all there.

Larry Martell

unread,
Oct 10, 2012, 7:23:45 PM10/10/12
to django...@googlegroups.com
I'm having trouble getting django to read my fixture file. I created it in yaml:

- model: cdsem.fields
pk: 1
fields:
name: data_file_id
description: data_file_id
- model: cdsem.fields
pk: 2
fields:
name: tool_id
description: tool_id

and so on.

I put it in a file called initial_data.yaml in the fixtures dir of my
app. When I run syncdb it does not pick it up. FIXTURE_DIRS was not
set. I should not have to set it, but I did anyway, but it still did
not pick up the file. If I run loaddata and give it the path to the
file I get:

Problem installing fixture './fixtures/initial_data': yaml is not a
known serialization format.

The docs say yaml is supported. What am I doing wrong?

On Thu, Oct 4, 2012 at 10:36 AM, Jonathan Baker

Joseph Wayodi

unread,
Oct 11, 2012, 3:37:22 AM10/11/12
to django...@googlegroups.com
On Thu, Oct 11, 2012 at 2:23 AM, Larry Martell <larry....@gmail.com> wrote:
> I'm having trouble getting django to read my fixture file. I created it in yaml:
>
> - model: cdsem.fields
> pk: 1
> fields:
> name: data_file_id
> description: data_file_id
> - model: cdsem.fields
> pk: 2
> fields:
> name: tool_id
> description: tool_id
>
> and so on.
>
> I put it in a file called initial_data.yaml in the fixtures dir of my
> app. When I run syncdb it does not pick it up. FIXTURE_DIRS was not
> set. I should not have to set it, but I did anyway, but it still did
> not pick up the file. If I run loaddata and give it the path to the
> file I get:
>
> Problem installing fixture './fixtures/initial_data': yaml is not a
> known serialization format.
>
> The docs say yaml is supported. What am I doing wrong?
>

According to the docs [1], you may need to install PyYAML:
<http://pypi.python.org/pypi/PyYAML>.

[1] <https://docs.djangoproject.com/en/1.4/topics/serialization/#serialization-formats>

Joseph.

Larry Martell

unread,
Oct 11, 2012, 6:51:49 AM10/11/12
to django...@googlegroups.com
Thanks! Didn't see that.

bb...@yahoo.com

unread,
Oct 12, 2012, 5:44:17 AM10/12/12
to django...@googlegroups.com
Hi Larry,
Do you have PyYAML installed? If not install it first and retry or alternatively you could just serialize your fixture in (my personal favorite) JSON.
Cheers!
Abraham.

--------------------------
Sent from my mobile device

Larry Martell

unread,
Oct 12, 2012, 8:51:52 AM10/12/12
to django...@googlegroups.com
On Fri, Oct 12, 2012 at 3:44 AM, <bb...@yahoo.com> wrote:
> Hi Larry,
> Do you have PyYAML installed? If not install it first and retry or alternatively you could just serialize your fixture in (my personal favorite) JSON.

The client decide to use XML.

Larry Martell

unread,
Oct 12, 2012, 2:09:10 PM10/12/12
to django...@googlegroups.com
So my client decided to use xml. I created the file and put it in
fixtures/initial_data.xml. On my development machine, which is a Mac,
it worked fine - running syncdb loaded the data. But on my client's
machine, which is running CentOS, running syncdb did not load the data
(it said 'Installed 0 object(s) from 0 fixture(s)'). But when I ran:

manage.py loaddata fixtures/initial_data.xml

That worked. Anyone have any ideas why sycndb worked on my mashine,
but it's not working on theirs?

Kurtis Mullins

unread,
Oct 12, 2012, 2:22:13 PM10/12/12
to django...@googlegroups.com
On Fri, Oct 12, 2012 at 2:09 PM, Larry Martell <larry....@gmail.com> wrote:

So my client decided to use xml. I created the file and put it in
fixtures/initial_data.xml. On my development machine, which is a Mac,
it worked fine - running syncdb loaded the data. But on my client's
machine, which is running CentOS, running syncdb did not load the data
(it said 'Installed 0 object(s) from 0 fixture(s)'). But when I ran:

manage.py loaddata fixtures/initial_data.xml

That worked. Anyone have any ideas why sycndb worked on my mashine,
but it's not working on theirs?

That's very weird. I haven't seen that affect off-hand. I've worked with Django on Linux (CentOS, Ubuntu, Debian) as well as OSX and it has always tended to work fine. I'm not sure on a solution other than to keep checking for inconsistencies or issues with the .xml file itself. Hopefully someone else who has ran into this problem before can chime in and offer some help. Good luck!

Larry Martell

unread,
Oct 12, 2012, 3:30:35 PM10/12/12
to django...@googlegroups.com
Yeah, I don't think it's platform dependent. The xml file was pulled
from our git repository, and it matches mine. And it works with
loaddata. I'll have to set a breakpoint and see why manage doesn't see
the file.

Kurtis Mullins

unread,
Oct 12, 2012, 4:24:48 PM10/12/12
to django...@googlegroups.com
Good luck to you! The code is here if you want to start crawling through it for a good point to place a breakpoint: https://github.com/django/django/blob/master/django/core/management/commands/syncdb.py

Kurtis Mullins

unread,
Oct 12, 2012, 4:25:45 PM10/12/12
to django...@googlegroups.com
Whoops, sorry that was the master branch. Here you go: https://github.com/django/django/blob/1.4.1/django/core/management/commands/syncdb.py

Larry Martell

unread,
Oct 13, 2012, 4:28:32 PM10/13/12
to django...@googlegroups.com
Thanks Kurtis. That's will save me a bit of time. I'm going to dig
into it now. I'll let you know what I find.

On Fri, Oct 12, 2012 at 2:25 PM, Kurtis Mullins

Larry Martell

unread,
Oct 13, 2012, 5:17:14 PM10/13/12
to django...@googlegroups.com
I didn't even need to use the debugger. I just ran with --verbosity 3,
and it showed me all the places it looked for fixtures files.
Strangely, it did not look for a fixtures dir immediately off my app's
main dir, but it looked for one everywhere else. My app is called
motor, and it lives in /usr/local/motor/motor. I had the initial_data
file in /usr/local/motor/motor/fixtures, and here is where it looked:

Checking '/usr/local/motor/motor/../motor/ui/fixtures' for fixtures...
Checking '/usr/local/motor/motor/../motor/dispatch/fixtures' for fixtures...
Checking '/usr/local/motor/motor/../motor/core/data/fixtures' for fixtures...
Checking '/usr/local/motor/motor/../motor/core/reports/fixtures' for fixtures...
Checking '/usr/local/motor/motor/../motor/core/alerts/fixtures' for fixtures...
Checking '/usr/local/motor/motor/../motor/app/semvision/fixtures' for
fixtures...
Checking '/usr/local/motor/motor/../motor/app/cdsem/fixtures' for fixtures...
Checking '/usr/local/motor/motor/../motor/app/developer/fixtures' for
fixtures...

I would have thought it would have looked in
/usr/local/motor/motor/fixtures. The reason it worked on my devel
system is that had I FIXTURE_DIRS set in my setting file, back from
when I was testing with the YAML files.

In any case, I'll just move the file to core/data/fixtures.
Reply all
Reply to author
Forward
0 new messages