App Engine数据存储区中的可选参考字段

时间:2018-10-21 06:41:38

标签: google-app-engine google-cloud-datastore app-engine-ndb

我有一个模特

    public class ExerciseAdapter extends CursorAdapter {

    Context context;

    public ExerciseAdapter(Context context, Cursor c) {
        super(context, c, 0);
        this.context = context;
    }


    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        return LayoutInflater.from(context).inflate(R.layout.list_item_exercise, parent, false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        int nameCol = cursor.getColumnIndex(WorkoutContract.WorkoutEntry.EXERCISE_NAME);
        String name = cursor.getString(nameCol);
        int weightCol = cursor.getColumnIndex(WorkoutContract.WorkoutEntry.WEIGHT);
        int weightInt = cursor.getInt(weightCol);
        String weight = String.valueOf(weightInt);
        int repCol = cursor.getColumnIndex(WorkoutContract.WorkoutEntry.REPS);
        int repsInt = cursor.getInt(repCol);
        String reps = String.valueOf(repsInt);
        int rpeCol = cursor.getColumnIndex(WorkoutContract.WorkoutEntry.RPE);
        int rpeInt = cursor.getInt(rpeCol);
        String rpe = String.valueOf(rpeInt);

        EditText exerciseName = (EditText) view.findViewById(R.id.exercise_name_table);
        exerciseName.setText(name);
        EditText exerciseWeight  = (EditText) view.findViewById(R.id.exercise_weight_table);
        exerciseWeight.setText(weight);
        EditText exerciseReps = (EditText) view.findViewById(R.id.exercise_reps_table);
        exerciseReps.setText(reps);
        EditText exerciseRpe = (EditText) view.findViewById(R.id.exercise_rpe_table);
        exerciseRpe.setText(rpe);

    }
}

    public class ExerciseActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {

    private ExerciseAdapter adapter;
    Uri exerciseUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exercise);


        ListView listView = findViewById(R.id.list);
        Intent intent = getIntent();
        exerciseUri = intent.getData();
        adapter = new ExerciseAdapter(this, null);
        listView.setAdapter(adapter);
        getLoaderManager().initLoader(0, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        String[] projections = {
                WorkoutContract.WorkoutEntry._ID,
                WorkoutContract.WorkoutEntry.DAY_ID,
                WorkoutContract.WorkoutEntry.EXERCISE_NAME,
                WorkoutContract.WorkoutEntry.WEIGHT,
                WorkoutContract.WorkoutEntry.REPS,
                WorkoutContract.WorkoutEntry.RPE
        };
        return new CursorLoader(
                this,
                exerciseUri,
                projections,
                null,
                null,
                null
        );
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        adapter.swapCursor(data);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        adapter.swapCursor(null);
    }
}

我建立了一个视图,可以在其中创建和修改这些模型实体,我希望class MyModel(ndb.Model): foo = ndb.StringProperty() class OtherModel(ndb.Model): baz = ndb.StringProperty() ref = ndb.KeyProperty(kind=MyModel) 的{​​{1}}字段是可选的,但是当我尝试提交并清空ref字段时或删除它的旧值,我得到OtherModel的验证错误

我的更新逻辑如下:

ref

我在哪里尝试将new_value设置为ndb.KeyProperty并为空字符串。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:2)

如果您没有Model实体密钥来将ref属性设置为不设置,即不要将该属性包含在您要传递回的实体值中用于写入的数据存储。如果这样做,您传递的属性值将受到完整性检查,并且将失败。

换句话说:根本不做

`setattr(item, property._name, new_value)`
ref不是键时,对于new_value

。而是按照以下方式做一些事情:

if hasattr(item, property._name):
   delattr(item, property._name)