有一些誤解。
首先,您在代碼中使用的是datastore
,而不是django。 datastore
沒有unique_together
和Meta
選項。 數據存儲是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)
EDIT2:
內置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)
噢,非常感謝您指出了:)請問您可以在某個方向指出我如何使用key_name? – 2013-05-12 04:15:53
請參閱我的編輯。我已經添加了一些示例代碼和參考數據存儲使用 – lucemia 2013-05-12 04:27:11
非常感謝你這樣一個啓發性的答案! – 2013-05-12 04:37:47