Update User
Update an existing user.
HTTP Request
PATCH/api/users/:id
Authorization
Authorization
- Required: Yes
- Permission: Staff or Admin
- Authentication: Token-based (Authorization: Token <your_api_key>)
Path Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| id | Integer | Yes | Unique ID of the user to update |
Request Body
| Field | Type | Required | Description |
|---|---|---|---|
| username | String | No | Unique username for the user. |
| mobile_number | String | No | Mobile phone number of the user. |
| String | No | Email address of the user (must be unique). | |
| first_name | String | No | First name of the user. |
| last_name | String | No | 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. |
| is_admin | Boolean | No | Whether user has admin privileges. |
| is_active | Boolean | No | Whether user account is active. |
| groups | Array | No | List of group IDs the user belongs to. |
| avatar | File | No | Avatar image file for the user. |
Example Requests
- 🐍 Python
- 🌐 Curl
1import requests
2
3api = requests.Session()
4api.headers.update({'Authorization': 'Token f4e75eab6e0f663a972d145478d6fc4b81762070'})
5response = api.patch(
6 'http://www.example.com/api/users/78/',
7 json={
8 'first_name': 'Michael',
9 'last_name': 'Chen-Rodriguez',
10 'mobile_number': '+1-555-0157',
11 'is_staff': True,
12 'groups': [1, 3, 5],
13 'avatar': open('path/to/avatar.jpg', 'rb')
14 }
15)1curl "http://www.example.com/api/users/78/" -X PATCH -H "Authorization: Token XXXXXXXXXXXXXXXXXXXXXX" -H "Content-Type: application/json" -d '{
2 "first_name": "Michael",
3 "last_name": "Chen-Rodriguez",
4 "mobile_number": "+1-555-0157",
5 "is_staff": true,
6 "groups": [1, 3, 5],
7 "avatar": "path/to/avatar.jpg"
8}'Status Codes
| Code | Description |
|---|---|
| 200 | User updated successfully |
| 400 | Bad request — invalid input |
| 401 | Unauthorized — authentication required |
| 403 | Forbidden — insufficient permissions |
| 404 | Not found — user does not exist |
| 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",
"full_name": "Michael Chen",
"gender": "male",
"avatar": "http://www.example.com/media/avatars/michael.jpg",
"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
- Set null value to avatar field to remove existing avatar.