2011-09-18 3 views
1

코드베이스를 app engine patch에서 django-nonrel을 사용하여 변환하는 문제를 조사하고 있습니다.app engine modelclass.properties()를 Django 함수로 바꾸는 방법은 무엇입니까?

이 코드베이스에서 여러 번 발생하는 것 중 하나는 엔티티의 모든 속성을 반복하는 것입니다. 예를 들어, 비교 자, 복사 생성자, __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 
관련 문제