且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

数据存储在localhost上,但不存储在gae数据存储上?

更新时间:2023-11-18 23:17:46

App Engine不支持Django模型。您必须使用App Engine的db.models或ndb.models API编写模型。



另一种选择是使用Django-nonrel。这是Django 1.3的一个分支,适用于App Engine或MongoDB。这将允许您使用Django模型。



它在本地成功有点奇怪。您的Django安装程序必须配置为使用SQLite之类的SQL数据库,它可能不会真正写入dev_appserver数据存储区。


For some reason, the form I created inserts data into the db on localhost. I deployed it to gae, the form works but nothing is been inserted into the datastore?

Here is my models file:

from django.db import models
# from django.contrib.auth.models import User

# Create your models here.
class Company(models.Model):
    id = models.IntegerField(primary_key=True);
    name = models.CharField(max_length=100, null=True);
    address = models.CharField(max_length=100, null=True);
    phone = models.CharField(max_length=15, null=True);
    website = models.CharField(max_length=50, null=True);
    email = models.EmailField(max_length=50, null=True);
    hiring = models.BooleanField(default=False);    
    latitude = models.DecimalField(max_digits=20, decimal_places=10, null=True);
    longitude = models.DecimalField(max_digits=20, decimal_places=10, null=True);
    approved = models.BooleanField(default=False);
    date_added = models.DateTimeField(auto_now_add=True);
    about_us = models.TextField(max_length=500, null=True);

My form:

from django import forms
from company.models import Company

class SignUpCompanyForm(forms.ModelForm):
    class Meta:
        model = Company;

My html form:

{% block content %} 
    <div id="content">
        <!-- a form for sign up -->
        <form id="sign" action="/sign_up_company" method="post">
            <table>{{ form }}</table>
            <input type="submit" value="Sign Up" />
        </form>
    </div>
{% endblock %}

My view function:

def sign_up(request):
    if request.method == 'POST':
        form = SignUpCompanyForm(request.POST);
        if form.is_valid():
            company = form.save(commit=False);
            company.save();
            # company.put();

    return HttpResponseRedirect('/confirmation');

The action of the form is mapped to "sign_up".

When I run "python manage.py syndb" I get:

C:\Users\Joe\Desktop\test>python manage.py syncdb
Creating tables ...
Installing custom SQL ...
Installing indexes ...
No fixtures found.
Exception AttributeError: "'NoneType' object has no attribute 'mkstemp'" in <bound method DatastoreFileStub.__del__ of <google.appengine.api.datastore_file_stub
.DatastoreFileStub object at 0x029874F0>> ignored

My app.yaml:

application: app_name
version: 1
runtime: python27
api_version: 1
threadsafe: yes

builtins:
- remote_api: on

inbound_services:
- warmup

libraries:
- name: django
  version: latest

handlers:
- url: /css
  static_dir: css

- url: /_ah/queue/deferred
  script: djangoappengine.deferred.handler.application
  login: admin

- url: /_ah/stats/.*
  script: djangoappengine.appstats.application

- url: /media/admin
  static_dir: django/contrib/admin/media
  expiration: '0'

- url: /.*
  script: djangoappengine.main.application

My index.yaml:

indexes:

- kind: django_admin_log
  properties:
  - name: user_id
  - name: action_time
    direction: desc

- kind: django_content_type
  properties:
  - name: app_label
  - name: name

App Engine does not support Django models. You have to write your models using App Engine's db.models or ndb.models API.

The other option is to use Django-nonrel. It's a fork of Django 1.3 that works on App Engine or MongoDB. This will allow you to use Django models.

It's a bit odd that it's succeeding locally. Your Django setup must be configured to use some SQL database like SQLite, it's probably not actually writing to the dev_appserver datastore.