开发者

Appengine Bulkloader with django-nonrel

I want to use appengine bulkloader 开发者_如何学Cwith my django-nonrel project as suggested in http://thomas.broxrost.com/category/google-app-engine/ and in http://blog.suinova.com/2009/03/tutorial-on-bulkloading-data-onto-app.html. But it giving error : No module named google.appengine.ext.webapp. I think this is because of Django-nonrel,I am not able to access google webapp.Correct me if I am wrong. Also, it seems my model fields are also different as supposrted by google bulkloader. Let me know if anyone knows any other alternative. Any online documents or pointers are welcome :-)


just saw your post. I'm able to use the appengine bulkloader just fine with my django-nonrel project.

There are a couple things to note:

  • On the models I need bulkloaded, I have the primary keys set manually by placing 'primary_key=True' in the field I want to use as a primary key. This makes Django create the model with a key_name of your primary key field and you can be sure the models you create will have known primary keys. (instead of letting the auto pk fields take control and then the pk is appengine's random id field)

  • in your bulkloader.yaml file, reference your foreign key property fields with 'field_id'. The way django constructs the db schema is foreign key fields are actually stored with the '_id' tacked on. Don't try to use the import_transform and export_transform to turn your field into an appengine 'key' field...

Here is an example of what I have that currently works.

class Team(models.Model):
    appengine_key = models.CharField(max_length=100, primary_key=True)
    abbr = models.CharField(max_length=3)
    name = models.CharField(max_length=10)
    division = models.ForeignKey(Division, related_name="teams")
    is_active = models.BooleanField(default=True)
    created_time = models.DateTimeField(auto_now_add=True)
    updated_time = models.DateTimeField(auto_now=True)

My bulkloader.yaml declaration for this field looks like this:

python_preamble:
- import: google.appengine.ext.bulkload.transform
- import: google.appengine.ext.db
- import: re
- import: base64
- import: datetime

transformers:
- kind: app_team
  connector: csv
  connector_options:
    encoding: utf-8
    columns: from_header

  property_map:
    - property: __key__
      external_name: appengine_key
      export_transform: transform.key_id_or_name_as_string

    - property: abbr
      external_name: abbr

    - property: name
      external_name: name

    - property: division_id
      external_name: division

    - property: is_active
      external_name: is_active
      import_transform: transform.none_if_empty(bool)

    - property: created_time
      external_name: created_time
      import_transform: transform.import_date_time('%m/%d/%Y %H:%M:%S')
      export_transform: transform.export_date_time('%m/%d/%Y %H:%M:%S')

    - property: updated_time
      external_name: updated_time
      import_transform: transform.import_date_time('%m/%d/%Y %H:%M:%S')
      export_transform: transform.export_date_time('%m/%d/%Y %H:%M:%S')

Then, your csv file just needs to have the field headers as you referenced them in the 'external_name' declaration, and the corresponding data to bulkload!

You can make sure it works on your local environment by running something similar to the following while your local runserver is running:

python2.5 /usr/local/bin/appcfg upload_data --config_file=app/bulkloader.yaml --filename=app/fixtures/teams.csv --kind=app_team --url=http://localhost:8080/_ah/remote_api app
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜