django - 如何在 django-piston 剩余输出中显示外键而不是相关对象数据

标签 django foreign-keys extjs4 django-piston

我使用最新的 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

关于django - 如何在 django-piston 剩余输出中显示外键而不是相关对象数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7493993/

相关文章:

python - 如何在 Django 管理中删除面包屑

mysql - 使用识别多对一关系是否意味着同一个表上必须有重复的主键和外键?

extjs - 在网格 sortchange 事件之前更改 store extraParams

javascript - 在 ext 网格中显示值,其中值对应于另一个表/模型中的字符串

javascript - 删除 ExtJs 4 默认类

mysql - 避免对 FK 表进行更改 Django Models 1.11

django cron 作业无法获取模型查询

mysql - SQL 错误 : #1005 - Can't create table

mysql - 为什么mysql允许删除外键引用的记录?

mysql - 每月最新条目