Skip to content

Updating single field of a model #1588

Description

@MeLight

I have model with multiple fields. The model in the Python App Engine app looks like this:

class Team(ndb.Model):
  field_a = ndb.StringProperty()
  field_b = ndb.StringProperty()
  field_c = ndb.IntegerProperty()

When running this code from the Node.js part of the app:

var key = ds.key(['Team', team_id]);
    ds.update({
      key: key,
      data: [{name: "field_c", value: new_count, excludeFromIndexes: true}]
    }, function(err) {
      if (!err) {
        // Record saved successfully.
      }
    });

I get fields field_a and field_b deleted from the model. How can I update only one field of the object without deleting the other fields?

Thanks

Activity

  1. stephenplusplus commented on Sep 12, 2016

    @stephenplusplus
    Contributor

    You have to do a manual "get and update", which would look like:

    var key = ds.key(['Team', team_id]);
    
    ds.get(key, function(err, entity) {
      if (err) {
        // Error handling omitted.
      }
    
      entity.data.field_c = newCount;
    
      ds.save({
        key: key,
        data: entity.data
      }, function(err) {
        if (!err) {
          // Record saved successfully.
        }
      });
    });
  2. MeLight commented on Sep 17, 2016

    @MeLight
    Author

    Thanks, that solved the overwriting issues, but it doesn't work with ComputedProperty. Any ideas how to make that work too?

  3. callmehiphop commented on Sep 19, 2016

    @callmehiphop
    Contributor

    @MeLight Did you try to fetch the data after doing a save? Per the upstream docs it looks like the stored value is ignored and they re-compute the property when you next read from Datastore.

  4. MeLight commented on Sep 24, 2016

    @MeLight
    Author

    In the same doc they have a scary red box claiming this:
    2016-09-24_1456

    So I decided to stop using that feature altogether, and calculating that property manually when needed :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

api: datastoreIssues related to the Datastore API.type: questionRequest for information or clarification. Not an issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions