Hi Alexandros

Your case sounds a little more advanced than the solution I've found.

But I think Toru can use it.

The reason i got the error, was that I had overridden the constructor
of Model to create a suitable key name.

The code went something like this.

class MyModel(Model):
  def __init__(self, key=None, key_name=None, name=None, **kw):
    key_name = 'k:%s' % name
    super(MyModel,self).__init__(self, key_name=key_name, name=name
**kw)

This centralizes key name creation. The datastore however provides the
key og
of the object in the datastore when it is fetched - and for some
reason, 1.3.1 now validates that
there is not both a key_name and a key (i guess) - and the code
fails..

So i changed my code something like this, avoiding the key_name
shortcut
class MyModel(Model):
  def __init__(self, key_name=None, name=None, **kw):
    if not key:
      key = Key(MyModel.kind(), 'k:%s' % name)
    super(MyModel,self).__init__(self, key=key, name=name, **kw)

I hope this i robust enough for future updates.

Cheers
Tonny



On Feb 11, 2:18 pm, d43m0n <[email protected]> wrote:
> I was getting the same error. It turns out that after 1.3.1
> (presumably), the function from_entity (which gets called after a
> fetch) in google/appengine/ext/db/__init__.py executes this line:
>
> instance = cls(None, _from_entity=True, **entity_values)
>
> which calls the constructor of your entity. In my case, as with Toru,
> the constructor would auto-generate a keyname if none was given... but
> if the query is based on keys, then we get this BadArgumentError.
>
> I don't know why the SDK is trying to (re-?)create my entities. The
> way I patched this was to find all points in my code where I would
> create entities without providing a keyname and modify them so I would
> provide a keyname by manually calling the generator function. In my
> case it looked something like this:
>
> entity = cls(key_name=cls.key_name_generator(**kwds), **kwds)
>
> Hope this helps,
> Alexandros
>
> On Feb 11, 5:00 am, Toru Tomita <[email protected]> wrote:
>
>
>
> > Yes, Me too,
>
> > I get same problem.
>
> > 1.3.1 current production env in /base/python_lib/versions/1/google/
> > appengine/ext/db/__init__.py in __init__ raises the error
>
> > 672:      if key_name and key_name != key.name():
> > 673:        raise BadArgumentError('Cannot use key and key_name at the
> > same time'
> > 674:                               ' with different values')
>
> > but 1.3.0 was  like this
>
> >       if key_name is not None:
> >             raise BadArgumentError('Cannot use key and key_name at the same
> > time')
>
> > My app uses OpenID GAE, and
> > -------------
> > class Session(db.Expando):
> >   # the logged in person
> >   person = db.ReferenceProperty(Person)
>
> >   # OpenID library session stuff
> >   openid_stuff = db.TextProperty()
>
> >   def __init__(self, parent=None, key_name=None, **kw):
> >     """if key_name is None, generate a random key_name so that
> >        session_id cookies are not guessable
> >     """
> >     if key_name is None:
> >       import uuid
> >       key_name = uuid.uuid4()
> >       import binascii
> >       key_name = binascii.unhexlify(key_name.hex)
> >       import base64
> >       key_name = "S" + base64.urlsafe_b64encode(key_name).rstrip('=')
> >     super(db.Expando, self).__init__(parent=parent, key_name=key_name,
> > **kw)    <<-----here raise the error
> > ------------
>
> > I cannot solve the problem....
>
> > On Feb 10, 6:04 am, Tonny <[email protected]> wrote:
>
> > > I'm getting this error this morning (CET)
>
> > > "Cannot use key and key_name at the same time with different values"
>
> > > The error happens when an Entity is loaded from the store.
>
> > > The app worked fine until recently (unfortunately I do not have an
> > > exact time, since it's a non default verison which is not visited
> > > every day.
>
> > > We haven't made any updates since the last time we saw it working, so
> > > what's changed?
>
> > > I noticed the server version says 1.3.1 in the log, but the latest
> > > stable download is 1.3.0 - i that correct?
>
> > > Regards
> > > Tonny

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en.

Reply via email to