2011-09-21 4 views
2

최신 django-piston 0.2.3rc1을 사용합니다 (단, 필요할 경우 다운 그레이드 할 수 있음). GET 요청에 대한 결과관련 객체 데이터 대신 장고 피스톤 레스트 출력에서 ​​외래 키를 표시하는 방법

class MaintenanceHandler(CsrfExemptBaseHandler): 
    allowed_methods = ('GET', 'POST', 'PUT', 'DELETE') 
    anonymous = is_anonymous = True 
    model = Maintenance 
    fields = ('maintenance_id', 'maintenance_client', 'maintenance_house', 'maintenance_type', 'maintenance_tariff', 'maintenance_date', 'maintenance_active') 
    exclude =() 


def read(self, request, id=None): 
    base = Maintenance.objects 

    if id: 
     return base.get(pk=id) 
    else: 
     try: 
      result = base 
      filters_list = simplejson.loads(request.GET.get('filter', '')) 
      for item in filters_list: 
       if item['property'] == u'maintenanceActive': 
        result = result.filter(maintenance_active__exact=item['value']) 
       if item['property'] == u'maintenanceClient': 
        result = result.filter(maintenance_client__exact=item['value']) 
       if item['property'] == u'maintenanceType': 
        result = result.filter(maintenance_type__exact=item['value']) 
       if item['property'] == u'maintenanceTariff': 
        result = result.filter(maintenance_tariff__exact=item['value']) 
     except: # если фильтров нет или какая-нибудь ошибка 
      result = base.all() 

     if 'result' not in locals(): # если фильтр есть, но ничего не применилось (пустой или не те) 
      result = base.all() 

     if 'start' in request.GET or 'limit' in request.GET: 
      start = request.GET.get('start', 0) 
      limit = request.GET.get('limit', 1) 
      result = result[start:limit] 

     return result 

(/ API/유지 보수 예를 들어,/8) 난을 참조하십시오

{ 
    "message": "Something good happened on the server!", 
    "data": { 
     "maintenance_type": 1, 
     "maintenance_tariff": 13, 
     "maintenance_active": true, 
     "maintenance_date": "2011-09-20 00:00:00", 
     "maintenance_house": 3, 
     "maintenance_id": 8, 
     "maintenance_client": 875, 
    }, 
    "success": true 
} 

는 그래서 그래, 그것은 좋은 기능입니다 :

{ 
    "message": "Something good happened on the server!", 
    "data": { 
     "maintenance_type": { 
      "type_name": "домофон", 
      "type_active": true, 
      "type_id": 1 
     }, 
     "maintenance_tariff": { 
      "tariff_id": 13, 
      "tariff_type": { 
       "type_name": "домофон", 
       "type_active": true, 
       "type_id": 1 
      }, 
      "tariff_name": "домофон 1", 
      "tariff_value": "435.00", 
      "tariff_active": true 
     }, 
     "maintenance_active": true, 
     "maintenance_date": "2011-09-20 00:00:00", 
     "maintenance_house": { 
      "house_street": "dasdasd", 
      "house_id": 3, 
      "house_number": 3, 
      "house_housing": 3, 
      "house_active": true, 
      "house_building": 3, 
      "house_district": "sdsadas" 
     }, 
     "maintenance_id": 8, 
     "maintenance_client": { 
      "client_contract_number": "PO77189393_7534", 
      "client_chief_accountant": "Gerrit", 
      "client_name": "Adrian66777", 
      "client_commission": "3558.00", 
      "client_chairman": "Scottie", 
      "client_active": true, 
      "client_debt": "530.00", 
      "client_contract_date": "2011-06-06", 
      "client_id": 875, 
      "client_comments": "5820", 
      "client_contract_index": "PO84741558_9604", 
      "client_manager": "Florian", 
      "client_contact": "9 Elm Waterford Drive" 
     } 
    }, 
    "success": true 
} 

내가 볼 것으로 예상 하지만 내 경우에는 (ExtJS와 Django 통합) 외래 키만 필요합니다. 관련 객체에 대한 데이터를 외래 키로 대체하는 방법은 무엇입니까?

답변

3

시도 maintenance_house_idmaintenance_house을 변경하고 유지 보수 핸들러에 classmethod를 추가 :

class MaintenanceHandler(CsrfExemptBaseHandler): 
    fields = ('maintenance_id', 'maintenance_client', 'maintenance_house_id', 'maintenance_type', 'maintenance_tariff', 'maintenance_date', 'maintenance_active') 

    @classmethod 
    def maintenance_house_id(self, instance): 
     # instance is a Maintenance object 
     return instance.maintenance_house.id 
2

내가 올바르게 모델의 질문을 이해한다면 당신은 사용해야

maintenance_house_id = property(lambda self: self.maintenance_house.id)