创建一个用户并为他分配一个组

时间:2014-11-12 16:30:47

标签: python json django tastypie

首次尝试创建用户:

data = {
    "username": "johndoe",
    "first_name": "john",
    "last_name": "doe",
    "email": "john@doe.com.br"
}

ApiFieldError:'群组'字段没有数据,也不允许空值。   *" POST / api / v1 / user /?format = json HTTP / 1.1" 500 *

第二次尝试创建用户

data = {
    "username": "johndoe",
    "first_name": "john",
    "last_name": "doe",
    "email": "johndoe@jd.com",
    "groups": [
        {
            "name": "GroupTest"
        }
    ]
}

NotFound:提供了错误的网址' n'对于' GroupResource'资源。 " POST / api / v1 / user /?format = json HTTP / 1.1" 500

在两次尝试中都创建了用户。 产生以下结果:

  

:8000 / api / v1 / user /?format = json

{
    "date_joined": "2014-11-12T14:02:00",
    "email": "john@doe.com.br",
    "first_name": "john",
    "groups": [],
    "id": 22,
    "is_active": true,
    "is_staff": false,
    "is_superuser": false,
    "last_login": "2014-11-12T14:02:00",
    "last_name": "doe",
    "resource_uri": "/api/v1/user/22/",
    "username": "johndoe"
}

{
    "date_joined": "2014-11-12T14:15:20",
    "email": "johndoe@jd.com",
    "first_name": "john",
    "groups": [],
    "id": 24,
    "is_active": true,
    "is_staff": false,
    "is_superuser": false,
    "last_login": "2014-11-12T14:15:20",
    "last_name": "doe",
    "resource_uri": "/api/v1/user/24/",
    "username": "johndoe"
}

如果我在django-admin中创建用户,结果是:

{
    "date_joined": "2014-11-12T14:13:47",
    "email": "johndoe@jd.com",
    "first_name": "john",
    "groups": [
        {
            "id": 1,
            "name": "GroupTest",
            "permissions": [], # //9 items
            "resource_uri": "/api/v1/groups/1/"
        }
    ],
        "id": 23,
        "is_active": true,
        "is_staff": false,
        "is_superuser": false,
        "last_login": "2014-11-12T14:13:47",
        "last_name": "doe",
        "resource_uri": "/api/v1/user/23/",
        "username": "johndoe"
}

我如何通过api创建用户,将他与一个现有的组相关联?就像当我用django-admin做的那样?

0 个答案:

没有答案