6

Update: I've noticed that entities are saved (and available at the Datastore Viewer) when I save them using views (and the create_object function). But when I use shell (manage.py shell) to create and save new entity it isn't commited to the storage (but still can be seen in Tes.objects.all()).


I started playing with the django-nonrel with google appengine and I am getting frustrated by thing as simple as saving Entities.

I've set up my environment as described in instruction. I managed to run sample application and it runs ok. I'd like to extend it so it saves my entity to the storage. To do so:

  1. I added new django module with models.py:

    from django.db import models
    
    class Tes(models.Model):
        name = models.CharField(max_length=150)
    
  2. I created a script to save some data:

    import os
    import sys
    sys.path.append("d:\\workspace\\project\\")
    os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
    from testmodule.models import Tes
    t = Tes(name="test")
    t.save()    
    tes = Tes.objects.all()
    for t in tes:
        print t.name
    

The script runs without errrors. When I run it few times one after another, it prints more and more "test" strings. But when I try running it after a minute break the Tes.objects.all() returns nothing. During that time the datastore file changes it size (but maybe that's just some kind of logs). When I look at the http://localhost:8000/_ah/admin/datastore I can select only AhAdminXrsfToken from select field.

Anyway, what am I missing? Where I can find some kind of logs which would tell me what's wrong?

szymond
  • 1,311
  • 2
  • 19
  • 41

2 Answers2

4

This is a gotcha that causes a lot of confusion. From the djangoappengine docs:

Also, never run manage.py runserver together with other management commands at the same time. The changes won't take effect. That's an App Engine SDK limitation which might get fixed in a later release.

So you can't do manage.py runserver and manage.py shell at the same time. If you do, changes to the datastore in one will not be visible in the other. There's a lock on the local datastore enforced by the App Engine SDK. Make sure you have stopped the server before you start the shell.

Wilfred Hughes
  • 29,846
  • 15
  • 139
  • 192
-1

Shoudn't it be t.put() if you are creating an entity rather than saving it? I use put() to create an entity and it works for me. And if you import django you may want to know that there are alternatives to django such as my choice GAE + Jinja2 + WTForms especially now that google.db.djangoforms is deprecated selecting a form framework for forms, a templating engine and perhaps a db framework and you do't have to import django which often results in forcing you to import much more than you need.

So my recommendation is to avoid import django... and instead use Jinja2 + WTForms. If you really want django on app engine then you might want to check in the project www.allbuttonspressed.com that enables all django for google app engine but be certain that you need this much django when I suspect all we need is a template engine and a form framework and we can do without django.

Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
  • 1
    This really doesn't answer the question. – Daniel Roseman Nov 22 '11 at 12:56
  • "What am I missing?" was asked and my answer is the difference between saving and creating. What I''m trying to say is that it can be the difference between saving and creating using save() and put() and that there are good alternatives to full django. – Niklas Rosencrantz Nov 22 '11 at 13:59