Question: This works flawlessly under Chrome, but in Firefox the fields don't populate until the SECOND time I hit submit on the page (after it comes back from the first time telling me invalid user/pass). What am I missing?

This is my code for the Greasemonkey script:

```
document.getElementById('loginBtn').addEventListener('click',
function (event) {
    document.getElementById('serverLogin').selectedIndex = "2";
    document.getElementById('usernameLogin').value = "username";
    document.getElementById('passwordLogin').value = "password";
    document.getElementById('loginForm').action = 'urltosubmit';
    location.href="javascript:(function(){ document.forms['loginForm'].submit(); })()";
},true);

```

Answer: 
The constructor, which is what you're using, always constructs a new entity. When you store it, it overwrites any other entity with the same key.

The alternate code you propose also has an issue: it's susceptible to race conditions. Two instances of that code running simultaneously could both determine that the entity does not exist, and each create it, resulting in one overwriting the work of the other.

What you want is the [Model.get\_or\_insert](http://code.google.com/appengine/docs/python/datastore/modelclass.html#Model_get_or_insert) method, which is syntactic sugar for this:

```
def get_or_insert(cls, key_name, **kwargs):
  def _tx():
    model = cls.get_by_key_name(key_name)
    if not model:
      model = cls(key_name=key_name, **kwargs)
      model.put()
    return model
  return db.run_in_transaction(_tx)

```

Because the get operation and the conditional insert take place in a transaction, the race condition is not possible.
Is this what you are looking for -> <http://code.google.com/appengine/docs/python/datastore/modelclass.html#Model_get_or_insert>