Loading data using the bootloader

In short: how can I configure bulkloader to insert data into 2 models with links?

I have a person and a fruit class, with a person associated with the fruit:

class Fruit(db.Model): 
    name = db.StringProperty()
class Person(db.Model): 
    name = db.StringProperty() 
    customer = db.ReferenceProperty(Fruit)

And I want to download this CSV data:

Name,Fruit
Bob,Banana
Joe,Apple
Tim,Banana

I tried using create_foreign_key, as in docs :

transformers:

- kind: fruit
  connector: csv
  property_map:
    - property: fruit
      external_name: Fruit

- kind: person
  connector: csv
  connector_options:
    encoding: utf-8
    columns: from_header
  property_map:
    - property: title
      external_name: Name
    - property: fruit
      external_name: Fruit
      import_transform: transform.create_foreign_key('fruit')

When I run the command:

appcfg.py upload_data --config_file=bulkloader.yaml --filename=food.csv --kind=person .

People are loading and they have foreign keys for fruits, but the fruit objects that they point to do not exist.

When I try --kind=fruit, fruits are loading, but there are many duplicates.

I try to connect a person with a fruit without having repeating fruits - is this possible through bulkloader?

+3
source share
3 answers

, , .

0

.

, . , , , . .

Banana Apple , transform.create_foreign_key('Fruit'). , - . , Fruit, , . __key__, .

-, . post_import_function, , , , , , .

+4

post_import_function.

. post_import_function, :

def fkeyLocation(input_dict, entity_instance, bulkload_state):
   entity_instance.availableAt =  Location.all().filter('name = ',input_dict['availableAt']).get().key()

   return entity_instance

The trick is to do a search using input_dict. If you use polymodels, you cannot use the automatically generated "view" from the wizard, you must use model.modelName from the sample code here .

+1
source

All Articles