2011-09-18 12 views
1

コードベースをapp engine patchからdjango-nonrelに変更する際の課題を検討しています。アプリエンジンmodelclass.properties()をDjango関数に置き換えるにはどうすればいいですか?

このコードベースで何度か起こることの1つは、エンティティのすべてのプロパティを繰り返し処理することです。たとえば、コンパレータ、コピーコンストラクタ、同等の__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 '' 

しかし、プロパティ()関数はgoogle.appengine.ext.db.Modelからです。

django-nonrelを使用したい場合、私のすべてのモデルオブジェクトはdjango.db.models.Modelからではなくなります。

そのクラスに相当する機能はありますか?

答えて

0

私は眠りにつき、おおよその答えで目を覚ましました。クラスメンバーの上に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 
関連する問題