Funyapu
Funyapu

Reputation: 3

Best way to initialize datastore w/ related entities

To run my app, I need some static entities,

so I've decided to upload them w/ bulkloader through Remote API from CSV files.

But I have some entities those have relationship in between. Like:

- kind: Category
  properties:
  - name: name

- kind: SubCategory
  ancestor: yes
  properties:
  - name: parent_id
  - name: name

How should I create a csv data to make it? Is there any other way that I should take to init my app datastore?

Upvotes: 0

Views: 76

Answers (1)

Brent Washburne
Brent Washburne

Reputation: 13158

If you define the key values (as strings) then you can create csv files with those values. One file would contain the Category values category_key,name. The other file would contain the SubCategory values subcategory_key,category_key,name. For example,

cat1,Category 1
cat2,Category 2

subcat1,cat1,SubCategory 1
subcat2,cat1,SubCategory 2
subcat3,cat2,SubCategory 3

You can read the files line by line, and create the static entities from the data like this (in Python):

import csv

with open('categories.csv') as csvfile:
    categories = csv.reader(csvfile)
    for row in categories:
        Category.get_or_insert(row[0], name=row[1])

with open('subcategories.csv') as csvfile:
    subcategories = csv.reader(csvfile)
    for row in subcategories:
        SubCategory.get_or_insert(row[0], parent_id=ndb.Key(Category, row[1]), name=row[2])

The parent_id value is constructed as a key. Both loops use the get_or_insert() function to prevent duplicate values so you can run it multiple times.

I see that SubCategory has an ancestor, so you could replace the last call with this (and remove the parent_id attribute):

        SubCategory.get_or_insert(row[0], parent=ndb.Key(Category, row[1]), name=row[2])

Upvotes: 1

Related Questions