Okay, I spent some more time with the flatpage and it works great. I just
needed to understand the syntax a little better. I also thought that I
needed to include a lot of cruft at the beginning of the dumpdata file that
basically amounted to logs and session_ids. Taking them out worked fine. I
was looking for the parts that reference the tables explicitly, and they
looked like this:

*** excerpt from dumpdata file, note that dumpdata uses the JSON format by
default ***
** front and closing brackets missing; this is just a sample of what the
data looks like **
{"pk": 1, "model": "v1.province", "fields": {"name": "Aki", "exits":
"26,20,5"}}, {"pk": 2, "model": "v1.province", "fields": {"name": "Awa",
"exits": "48,3,57,27"}}, {"pk": 1, "model": "v1.room", "fields":
{"province": 1, "name": "Bridge"}}, {"pk": 2, "model": "v1.room", "fields":
{"province": 1, "name": "Castle"}}, {"pk": 3, "model": "v1.room", "fields":
{"province": 1, "name": "Countryside"}},
*** / excerpt ***

So for those who are interested, I created my tables by hand (in the admin
view) for two of the entries. Then I used dumpdata to extract the info. I
then opened the file in notepad and looked for the table data I had added,
discarding everything else and being sure to keep the opening and closing
brackets, [].

Then I copy the lines I am keeping and use them as a reference to write a
function that fills in the different data for the rest of the table. In my
example, I am filling in data for a Province model which is linked to a Room
model (rooms are inside of provinces, conceptually). Each Province also has
a list of Exits (bordering provinces). I chose to include exits as a
CommaSeparatedIntegerField so there is not a separate model entry for exits.

Since the dumpdata file separates the models, I did the same in my
functions. I have a function that generates the strings for the province
models and another function that generates the fields for provinces
(including rooms and exits). They look approximately like this:

*** code ***
def getProvinceString(key, exits):
""" province string for loaddata """
    for num in exits:
        if num != exits[-1]:   # add a comma if this is not the last entry
in exits
            exit_string = exit_string + str(num) + ","
        else:
            exit_string = exit_string + str(num)
    province_string = """{"pk": %s, "model": "v1.province", "fields":
{"name": "%s", "exits": "%s"}}, """ % (pnum[key], key, exit_string)
    return province_string

def getFieldsString(key):
    """ field string for loaddata """
    global pk_count
    places = sorted(["Road", "Dojo", "Temple", "Market", "Bridge",
"Magistrate", "Castle", "Countryside", "Forest"])
    fields_string = ""
    for place in places:
        fields_string = fields_string + """{"pk": %d, "model": "v1.room",
"fields": {"province": %d, "name": "%s"}}, """ % (pk_count, pnum[key],
place)
        pk_count += 1
    return fields_string
*** / code ***

pk_count keeps track of which primary_key I am on so that I ensure that all
the keys are unique and sequential. Since I call getFieldsString multiple
times I need to keep the pk_count marching forward. I didn't need to do this
on getProvinceString because they already had province_ids from a dictionary
I had constructed earlier called pnum.

I then just loop through and write the strings. Note that in JSON there are
no end line characters needed, so just write every string after the previous
one. When generating the strings, a comma and space was added to each line
as delimiters.

*** code ***
f = open('provdata.json', 'w')
f.flush()
f.write("[")    # opening bracket for loaddata syntax
for key in provlist:
    string = getProvinceString(key, pexits[key])
    f.write(string)

pk_count = 1
for key in provlist:
    string = getFieldsString(key)
    f.write(string)
f.close()
*** / code ***

Then I need to strip the last comma from the file and add the closing
bracket. There is probably a more elegant way to do this, but this is what
came to mind first. I open the file, read it in, slice the buffer I read in,
and then re-write to the same file with the closing bracket.

*** code ***
f = open('provdata.json', 'r')
buff = f.read()
f.close()
buff = buff[:-2]

f = open('provdata.json', 'w')
f.flush()
f.write(buff)
f.write("]")
f.close()
*** / code ***

This yields a provdata.json flatpage which I copy into
myproject/myapp/fixtures
Then I run manage.py loaddata provdata.json and all of the provinces for my
game get loaded.

MUCH easier than coding them all by hand. And now that I have the general
method I can apply it to all my database tables.

Hope this helps someone.

-Tim


On Fri, Feb 19, 2010 at 1:09 PM, Timothy Kinney <timothyjkin...@gmail.com>wrote:

> I started playing with this last night and got about as far as you
> mentioned here. What I want to be able to do, though, is write a python
> script that writes an authentic json flatpage that I can import into the
> database. I have a large textfile full of names, items, and province
> information (for a game) and I want to shove all this into the database.
>
> I tried adding some provinces to the database, dumpdata, and then looked at
> the json file to see if I could copy the syntax it was using. This hasn't
> quiet worked yet, but I will keep trying.
>
> Has anyone tried what I want to do?
>
> -Tim
>
>
>
> On Fri, Feb 19, 2010 at 11:13 AM, Rick Caudill <cau0...@gmail.com> wrote:
>
>> Hi all,
>>
>> I just came across a need to provide initial data for flatpages.  I have
>> read before that people have asked to do this.  It is easier than thought
>> :)
>>
>> 1. Create your content via the admin interface
>> 2. Run  'python manage.py dumpdata flatpages > data.json'
>> 3. When you want to provide the data run 'python manage.py loaddata
>> data.json'
>>
>>
>> I hope this helps someone that needs to do this sort of thing.
>>
>>
>> --
>> Rick Caudill
>>
>>  --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To post to this group, send email to django-us...@googlegroups.com.
>> To unsubscribe from this group, send email to
>> django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com>
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/django-users?hl=en.
>>
>
>

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

Reply via email to