如何在django-活塞rest输出中显示外键而不是相关对象数据



我使用最新的django-piston 0.2.3rc1(但如果需要可能会降级)。

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

作为GET请求(例如/api/maintenance/8)的结果,我看到:

{
    "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
}

我期望看到:

{
    "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
}

所以,是的,这是一个很好的功能,但在我的情况下(Django与ExtJS集成),我只需要外键。如何将相关对象的数据替换为外键?

尝试将maintenance_house更改为maintenance_house_id并在维护处理程序中添加类方法:

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

如果我在你的模型中正确理解你的问题,你应该使用

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

最新更新