Create User
Create a new user.
HTTP Request
POST/api/users
Authorization
Authorization
- Required: Yes
- Permission: Staff or Admin
- Authentication: Token-based (Authorization: Token <your_api_key>)
Request Body
| Field | Type | Required | Description |
|---|---|---|---|
| username | String | Yes | Unique username for the user. |
| password | String | Yes | Password for the user account. |
| mobile_number | String | No | Mobile phone number of the user. |
| String | Yes | Email address of the user (must be unique). | |
| first_name | String | Yes | First name of the user. |
| last_name | String | Yes | Last name of the user. |
| gender | String | No | Gender of the user (`male`, `female`, non-binary). |
| is_staff | Boolean | No | Whether user has staff privileges (default: false). |
| is_admin | Boolean | No | Whether user has admin privileges (default: false). |
| is_active | Boolean | No | Whether user account is active (default: true). |
| groups | Array | No | List of group IDs the user belongs to. |
Example Requests
- 🐍 Python
- 🌐 Curl
1import requests
2
3api = requests.Session()
4api.headers.update({'Authorization': 'Token f4e75eab6e0f663a972d145478d6fc4b81762070'})
5response = api.post(
6 'http://www.example.com/api/users/',
7 json={
8 'username': 'michael_chen',
9 'password': 'SecurePass123!',
10 'mobile_number': '+1-555-0156',
11 'email': 'michael.chen@techcorp.com',
12 'first_name': 'Michael',
13 'last_name': 'Chen',
14 'gender': 'male',
15 'is_staff': True,
16 'is_admin': False,
17 'is_active': True,
18 'groups': [1, 3]
19 }
20)1curl "http://www.example.com/api/users/" -X POST -H "Authorization: Token XXXXXXXXXXXXXXXXXXXXXX" -H "Content-Type: application/json" -d '{
2 "username": "sarah_williams",
3 "password": "SecurePass456!",
4 "mobile_number": "+1-555-0189",
5 "email": "sarah.williams@techcorp.com",
6 "first_name": "Sarah",
7 "last_name": "Williams",
8 "gender": "female",
9 "is_staff": false,
10 "is_admin": false,
11 "is_active": true,
12 "groups": [2, 4]
13}'Status Codes
| Code | Description |
|---|---|
| 201 | User created successfully |
| 400 | Bad request — invalid input |
| 401 | Unauthorized — authentication required |
| 403 | Forbidden — insufficient permissions |
| 500 | Internal server error |
Response Fields
| Field | Type | Description |
|---|---|---|
| id | Integer | Unique ID of the user. |
| username | String | Username of the user. |
| mobile_number | String | Mobile phone number of the user. |
| String | Email address of the user. | |
| avatar | String | URL to the user's avatar image (null if none). |
| first_name | String | First name of the user. |
| last_name | String | Last name of the user. |
| full_name | String | Full name (first + last name). |
| gender | String | Gender of the user.(male/female) |
| is_staff | Boolean | Whether user has staff privileges. |
| is_admin | Boolean | Whether user has admin privileges. |
| is_active | Boolean | Whether user account is active. |
| last_login | String (ISO 8601) | Timestamp of last login (null for new users). |
| created_at | String (ISO 8601) | Timestamp when user was created. |
| updated_at | String (ISO 8601) | Timestamp when user was last updated. |
| is_online | Boolean | Whether user is currently online. |
| groups_data | Array[Object] | Array of group objects with detailed info. |
Group Data Structure
| Field | Type | Description |
|---|---|---|
| id | Integer | Unique ID of the group |
| name | String | Name of the group |
Example Response
{
"id": 42,
"username": "michael_chen",
"mobile_number": "+1-555-0156",
"email": "michael.chen@techcorp.com",
"first_name": "Michael",
"last_name": "Chen",
"gender": "male",
"avatar": "http://www.example.com/media/avatars/michael.jpg",
"full_name": "Michael Chen",
"is_staff": true,
"is_admin": false,
"is_active": true,
"last_login": null,
"created_at": "2023-01-01T12:00:00Z",
"updated_at": "2023-01-01T12:00:00Z",
"is_online": false,
"groups_data": [
{
"id": 1,
"name": "Admins"
},
{
"id": 3,
"name": "Editors"
}
]
}
Notes
- The
usernameandemailfields must be unique. - The
full_namefield is automatically generated fromfirst_nameandlast_name. - The
groupsfield should contain valid group IDs that already exist in the system.