Django休息框架CRUD API用于具有foriegn关键元素的模型

时间:2016-08-25 07:54:02

标签: django django-rest-framework crud django-serializer

好的,我在这里遇到了Django面临的另一个问题。这次是REST框架。

我的项目中有一个具有外键依赖项的模型。我必须将外键字段作为对象检索,而不仅仅是ID字段,并使用Serializer Relations成功完成。

所以我的 Serializer.py 看起来像是:

class EventSerializer(serializers.ModelSerializer):
    owner = EventUserSerializer(read_only=False)
    severity = EventSeveritySerializer(read_only=False)
    type = EventTypeSerializer(read_only=False)
    cause = EventCauseSerializer(read_only=False)
    subcause = EventSubcauseSerializer(read_only=False)
    impact = EventImpactSerializer(read_only=False)
    point_location = PointLocationSerializer(read_only=False)

    class Meta:
        model = models.Event
        fields = ('id', 'description', 'owner', 'status', 
            'severity', 'type', 'cause', 'subcause', 'impact', 
            'is_public', 'occurrence_time', 'reporting_agency', 
            'estimated_duration', 'confirmed_timestamp', 
            'rejected_timestamp', 'closed_timestamp', 'point_location', )

发出GET请求,我得到了我想要的结果:

{
    "meta": {
    "count": 1,
    "previous": null,
    "next": null
},
"results": [
{
    "id": 8,
    "description": "test event 1",
    "owner": {
        "user": {
            "id": 1,
            "username": "venus",
            "email": "venus.saini@ibigroup.com"
        }
    },
    "status": "confirmed",
    "severity": {
        "id": 2,
        "created": "2016-08-24T07:27:24.722000Z",
        "modified": "2016-08-24T07:27:24.723000Z",
        "name": "Severe",
        "is_enabled": true,
        "sortorder": 1
    },
    "type": {
        "id": 2,
        "created": "2016-08-24T07:27:45.203000Z",
        "modified": "2016-08-24T07:27:45.204000Z",
        "name": "Accident",
        "event_category": "unplanned",
        "sortorder": 2
    },
    "cause": {
        "id": 2,
        "created": "2016-08-24T07:28:16.088000Z",
        "modified": "2016-08-24T07:28:16.092000Z",
        "name": "Whether Conditions",
        "sortorder": 3,
        "event_type": 2
    },
    "subcause": {
        "id": 2,
        "created": "2016-08-24T07:28:28.475000Z",
        "modified": "2016-08-24T07:28:28.478000Z",
        "name": "Slippery",
        "display_name": "Slippery",
        "sortorder": 4,
        "cause": 2
    },
    "impact": {
        "id": 2,
        "created": "2016-08-24T07:28:40.599000Z",
        "modified": "2016-08-24T07:28:40.599000Z",
        "name": "All Lanes Blocked",
        "sortorder": 5
    },
    "is_public": false,
    "occurrence_time": "2016-08-25T06:34:13Z",
    "reporting_agency": "",
    "estimated_duration": null,
    "confirmed_timestamp": "2016-08-25T06:34:15Z",
    "rejected_timestamp": "2016-08-25T06:34:16Z",
    "closed_timestamp": "2016-08-25T06:34:18Z",
    "point_location": {
        "id": 2,
        "created": "2016-08-24T07:18:03.017000Z",
        "modified": "2016-08-24T07:18:03.018000Z",
        "roadway_name": "Northbound",
        "direction": "North",
        "is_primary": false,
        "cross_street_name": "test street 1",
        "offset": "After",
        "distance": 3,
        "is_forward": false
    }
 }]
 }

我遇到的问题是在使用POST请求时。我不知道那是怎么回事,所以我找到了this link。看起来它有解决方案;我的意思是这正是我想要实现的[传递外键元素的ID使对象输入]。但我不断收到像super(BaseSerializer, self).__init__(**kwargs) TypeError: __init__() got an unexpected keyword argument 'is_relation'这样的随机错误。

有人可以指导我如何做吗?我正在使用Postman来测试我的API。

提前致谢。

编辑:在没有CustomSerializer的情况下使用PUT / POST会给我错误415:

{
  "detail": "Unsupported media type \"text/plain;charset=UTF-8\" in request."
}

0 个答案:

没有答案