有一些误会。
首先,您在代码中使用 datastore,而不是 django。 datastore 没有 unique_together 或 Meta 选项。
Datastore 是 appengine 上的 nosql 服务。
如果你想让数据存储实体是唯一的。最简单的方法是使用key_name。
key_name 将是唯一的。后一个实体将替换旧实体,而它们具有相同的key_name。
例如:
# key_name has length limit (500), hash it can make sure it won't exceed the limit
log = Log(
key_name=str(hash((name,location,msg))),
name=name,
location=location,
msg=msg
)
log.put()
# it will replace any exist log which has the same name, location, msg combination.
# and the item can be Retrieve via key_name directly.
log = Log.get(key_name)
编辑2:
内置的hash 可能在不同的机器上返回不同的值。所以最好改用hashlib。
您可以通过多种方式定义您的key_name,只要确保它不会发生意外碰撞。
例如:
md5:http://docs.python.org/2/library/md5.html
或者只是将所有字段附加在一起。 key_name=name + "|" + location + "|" + msg
更多信息:
https://developers.google.com/appengine/docs/python/datastore/entities#Retrieving_an_Entity
如果你想在应用引擎上使用django,模型应该定义为:
from django.db import models
class Log(models.Model):
name = models.CharField(max_length=255)
location = models.StringProperty(max_length=255)
msg = models.StringProperty(max_length=255)
class Meta:
unique_together = (("name","location","msg"),)
EDIT3:
这是一个完整的例子,一个用于 db,另一个用于 ndb。
对于ndb,这很简单。对于db来说,有点难。
from google.appengine.ext import db
from google.appengine.ext import ndb
import webapp2
class Log(db.Model):
name = db.StringProperty()
location = db.StringProperty()
msg = db.StringProperty()
count = db.IntegerProperty()
@classmethod
def key_name(cls, name, location, msg):
return name+"|"+location+"|"+msg
@classmethod
def get(cls, name, location, msg):
return db.get(db.Key.from_path(cls.__name__, cls.key_name(name, location, msg) ))
class nLog(ndb.Model):
name = ndb.StringProperty()
location = ndb.StringProperty()
msg = ndb.StringProperty()
count = ndb.IntegerProperty()
class Test1(webapp2.RequestHandler):
def get(self):
name='test_name'
location = 'test_location'
msg = 'test_msg'
Qkey_name= Log.key_name(name, location, msg)
log = Log(
key_name=Qkey_name,
name=name,
location=location,
msg=msg,
count=0
).put()
if Log.get(name, location, msg) is not None:
Qcount = Log.get(name, location, msg).count
else:
Qcount = 1
class Test2(webapp2.RequestHandler):
def get(self):
name='test_name'
location = 'test_location'
msg = 'test_msg'
Qkey_name = name + "|" + location + "|" + msg
log = nLog(
id=Qkey_name,
name=name,
location=location,
msg=msg,
count=0
).put()
if nLog.get_by_id(Qkey_name) is not None:
Qcount = nLog.get_by_id(Qkey_name).count
else:
Qcount = 1
app = webapp2.WSGIApplication([
(r'/1', Test1),
(r'/2', Test2)
], debug=True)