开发者

How to show foreign key in django-piston rest output instead of related object data

开发者 https://www.devze.com 2023-04-06 02:14 出处:网络
I use latest django-piston 0.2.3rc1 (but may downgrade if needed). class MaintenanceHandler(CsrfExemptBaseHandler):

I use latest django-piston 0.2.3rc1 (but may downgrade if needed).

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

As result on GET request (/api/maintenance/8 for example) I see:

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

I expect to see:

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

So yes, it's nice feature but in my case (Django integration with ExtJS) I need only foreign keys.. How to replace data about related objects to foreign keys?


Try changing maintenance_house to maintenance_house_id and adding a classmethod to your maintenance handler:

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


If I understand your question correctly in your model you should use

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号