There are some misunderstanding.
Firstly, you are using datastore
in your code, not django. There is no unique_together
nor Meta
options for datastore
.
Datastore is a nosql service on appengine.
If you want to make datastore entity to be unique. The easiest way is using key_name
.
key_name will be unique. The later entity will replace the old one while they have the same key_name
.
For example:
# 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:
built-in hash
may return different value in different machine. So it is better to use hashlib instead.
you can defined your key_name
in many ways, just make sure it won't collision in accident.
for example:
md5: http://docs.python.org/2/library/md5.html
or just append all field together. key_name=name + "|" + location + "|" + msg
for more information:
https://developers.google.com/appengine/docs/python/datastore/entities#Retrieving_an_Entity
If you want to use django on app engine, the model should be defined as:
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:
Here is a complete example, one for db and the other for ndb.
For ndb, it is quite simple. For db, it is a little bit hard.
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)