如何用Django函数替换应用引擎modelclass.properties()?

如何用Django函数替换应用引擎modelclass.properties()?,django,google-app-engine,django-nonrel,Django,Google App Engine,Django Nonrel,我正在研究将代码库从使用转换为使用的挑战 在这个代码库中多次发生的事情之一是迭代实体的所有属性。例如,comparator、copy构造函数、str等 简化示例: def compare_things(thing_a, thing_b): '''Compare two things on properties not in Thing.COMPARE_IGNORE_PROPS''' if type(thing_a) != type(thing_b): return "Internal e

我正在研究将代码库从使用转换为使用的挑战

在这个代码库中多次发生的事情之一是迭代实体的所有属性。例如,comparator、copy构造函数、str等

简化示例:

def compare_things(thing_a, thing_b):
  '''Compare two things on properties not in Thing.COMPARE_IGNORE_PROPS'''
  if type(thing_a) != type(thing_b): return "Internal error"

  for prop in Thing.properties():
    if prop not in Thing.COMPARE_IGNORE_PROPS:
      attr_a = getattr(thing_a, prop)
      attr_b = getattr(thing_b, prop)
      if attr_a != attr_b:
        return prop + ": " + str(attr_a) + " is not equal to " + str(attr_b)
  return ''
但是,properties函数来自google.appengine.ext.db.Model

如果我希望使用django nonrel,那么我的所有模型对象都将来自django.db.models.model


那门课有没有同等的功能?

我睡着了,醒来时得到了一个大致的答案。可以使用dir在类成员上循环。类似于未经测试:

from django.db import models

def properties(model_class):
  ret = []
  for name in dir(model_class):
    thing = getattr(model_class, name)
    if (isinstance(thing, models.Field)):
      ret.append(name)
  return ret